• <legend id='NTYwV'><style id='NTYwV'><dir id='NTYwV'><q id='NTYwV'></q></dir></style></legend>

        <bdo id='NTYwV'></bdo><ul id='NTYwV'></ul>

      1. <tfoot id='NTYwV'></tfoot>

        <small id='NTYwV'></small><noframes id='NTYwV'>

      2. <i id='NTYwV'><tr id='NTYwV'><dt id='NTYwV'><q id='NTYwV'><span id='NTYwV'><b id='NTYwV'><form id='NTYwV'><ins id='NTYwV'></ins><ul id='NTYwV'></ul><sub id='NTYwV'></sub></form><legend id='NTYwV'></legend><bdo id='NTYwV'><pre id='NTYwV'><center id='NTYwV'></center></pre></bdo></b><th id='NTYwV'></th></span></q></dt></tr></i><div id='NTYwV'><tfoot id='NTYwV'></tfoot><dl id='NTYwV'><fieldset id='NTYwV'></fieldset></dl></div>
      3. 子类 UIButton 添加属性

        时间:2023-07-09

            <bdo id='viRDu'></bdo><ul id='viRDu'></ul>
              <legend id='viRDu'><style id='viRDu'><dir id='viRDu'><q id='viRDu'></q></dir></style></legend>

            1. <i id='viRDu'><tr id='viRDu'><dt id='viRDu'><q id='viRDu'><span id='viRDu'><b id='viRDu'><form id='viRDu'><ins id='viRDu'></ins><ul id='viRDu'></ul><sub id='viRDu'></sub></form><legend id='viRDu'></legend><bdo id='viRDu'><pre id='viRDu'><center id='viRDu'></center></pre></bdo></b><th id='viRDu'></th></span></q></dt></tr></i><div id='viRDu'><tfoot id='viRDu'></tfoot><dl id='viRDu'><fieldset id='viRDu'></fieldset></dl></div>
                  <tbody id='viRDu'></tbody>
                <tfoot id='viRDu'></tfoot>

                1. <small id='viRDu'></small><noframes id='viRDu'>

                  本文介绍了子类 UIButton 添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想继承 UIButton 以添加一些我需要的属性(不是方法...只有属性).

                  I'd like to subclass UIButton to add some properties that i need (not methods... only properties).

                  这里是我的子类的代码:

                  Here the code of my subclass:

                  //.h-----------------------
                  @interface MyButton : UIButton{
                      MyPropertyType *property;
                  }
                  
                  @property (nonatomic,retain) MyPropertyType *property;
                  @end
                  
                  //.m--------------------------
                  @implementation MyButton
                  @synthesize property;
                  
                  @end
                  

                  这里是我如何使用这个类:

                  And here how I use the class:

                  MyButton *btn = ((MytButton *)[MyButton buttonWithType:UIButtonTypeRoundedRect]);
                  btn.property = SomeDataForTheProperty;
                  

                  我从哪里得到这个错误:

                  From where i obtain this error :

                   -[UIRoundedRectButton setProperty:]: unrecognized selector sent to instance 0x593e920
                  

                  因此,从 ButtonWithType 我获得了一个 UIRoundedRectButton 并且 (Mybutton *) 不能转换它...我必须做什么才能获得 MyButton 对象?-init 是唯一的解决方案吗?

                  Thus, from ButtonWithType i obtain a UIRoundedRectButton and (Mybutton *) can't cast it... What i have to do to obtain a MyButton object ? is -init the unique solution ?

                  谢谢!

                  推荐答案

                  尝试使用带有 关联引用.它更简洁,适用于 UIButton 的所有实例.

                  Try using a category with Associative References instead. It is much cleaner and will work on all instances of UIButton.

                  UIButton+Property.h

                  UIButton+Property.h

                  #import <Foundation/Foundation.h>
                  
                  @interface UIButton(Property)
                  
                  @property (nonatomic, retain) NSObject *property;
                  
                  @end
                  

                  UIButton+Property.m

                  UIButton+Property.m

                  #import "UIButton+Property.h"
                  #import <objc/runtime.h>
                  
                  @implementation UIButton(Property)
                  
                  static char UIB_PROPERTY_KEY;
                  
                  @dynamic property;
                  
                  -(void)setProperty:(NSObject *)property
                  {
                      objc_setAssociatedObject(self, &UIB_PROPERTY_KEY, property, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
                  }
                  
                  -(NSObject*)property
                  {
                      return (NSObject*)objc_getAssociatedObject(self, &UIB_PROPERTY_KEY);
                  }
                  
                  @end
                  

                  //示例用法

                  #import "UIButton+Property.h"
                  
                  ...
                  
                  UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                  button1.property = @"HELLO";
                  NSLog(@"Property %@", button1.property);
                  button1.property = nil;
                  NSLog(@"Property %@", button1.property);
                  

                  这篇关于子类 UIButton 添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:突出显示时如何更改 UIButton 的背景颜色? 下一篇:UIButton:使点击区域大于默认点击区域

                  相关文章

                  <small id='csf1I'></small><noframes id='csf1I'>

                  <legend id='csf1I'><style id='csf1I'><dir id='csf1I'><q id='csf1I'></q></dir></style></legend>

                  • <bdo id='csf1I'></bdo><ul id='csf1I'></ul>
                  <tfoot id='csf1I'></tfoot>

                  1. <i id='csf1I'><tr id='csf1I'><dt id='csf1I'><q id='csf1I'><span id='csf1I'><b id='csf1I'><form id='csf1I'><ins id='csf1I'></ins><ul id='csf1I'></ul><sub id='csf1I'></sub></form><legend id='csf1I'></legend><bdo id='csf1I'><pre id='csf1I'><center id='csf1I'></center></pre></bdo></b><th id='csf1I'></th></span></q></dt></tr></i><div id='csf1I'><tfoot id='csf1I'></tfoot><dl id='csf1I'><fieldset id='csf1I'></fieldset></dl></div>