且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS-在操作中传递参数:@selector()

更新时间:2023-11-30 13:42:52

为什么不创建Custom UIButton class并将对象作为属性?

Why don't you make a Custom UIButton class and have the object as property?

请参阅下文.

"MyButton.h"

@interface MyButton : UIButton
@property(nonatomic, strong)MyClass *obj;
@end

"MyButton.m"

#import "MyButton.h"

@implementation MyButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

@end

现在将MyButton class分配给单元格/中的实际按钮,或者初始化自定义按钮,而不是普通的UIButton class并直接分配对象.

Now assign MyButton class to your actual button in the cell/ or initialize the custom button instead of normal UIButton class and assign the object directly.

在您的IBAction中,其中sender=MyButton

- (void) quantityDown:(id)sender{
   MyButton *btn  = (MyButton *)sender;
   //You get the object directly
   btn.obj;
}

通过这种方式,您实际上可以轻松地访问任意数量的属性.并且它在其他实现中也很有用.

Doing it this way you can actually access as many properties you want easily. And its useful in other implementations too.

希望有帮助.