iOS UIButton 传递多个参数

今天项目中遇到这样一个情况,一个tableView的cell上有一个button,button的触发事件我需要知道当前button所在cell的一些值和上次点击的cell的一些值,那么怎么把这些值和当前这个button绑定呢,OC提供了一个关联对象的方法,

objc_setAssociatedObject(id _Nonnull object, const void * _Nonnull key,
                         id _Nullable value, objc_AssociationPolicy policy)

 这是设置关联对象,第一个参数是要和谁关联的对象,例如button,第二个是key值,第三个是关联的对象,也就是你要绑定在button上的参数,最后一个是关联方式。

/** 
 * Returns the value associated with a given object for a given key.
 * 
 * @param object The source object for the association.
 * @param key The key for the association.
 * 
 * @return The value associated with the key \e key for \e object.
 * 
 * @see objc_setAssociatedObject
 */
OBJC_EXPORT id _Nullable
objc_getAssociatedObject(id _Nonnull object, const void * _Nonnull key)
    OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0);

这是获取关联对象的方法。第一个参数是绑定的对象,第二个是key值。这样就能传递多个参数。下面是例子:

cell.selectBtn.tag = indexPath.row;
    objc_setAssociatedObject(cell.selectBtn, @"index", indexPath,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    objc_setAssociatedObject(cell.selectBtn, @"cell", cell,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    objc_setAssociatedObject(cell.selectBtn, @"table", tableView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [cell.selectBtn addTarget:self action:@selector(defaultButtonClick:) forControlEvents:UIControlEventTouchUpInside];

我给button绑定了三个参数,一个indexpath,一个cell,一个tableView。

下面是获取这三参数的方法:

-(void)defaultButtonClick:(UIButton *)btn{
    
    NSIndexPath * indexPath = objc_getAssociatedObject(btn, @"index");
    MyCarTableViewCell *cell = objc_getAssociatedObject(btn, @"cell");
    DLog(@"======%ld=====%@",(long)indexPath.row,cell.defaultLab.text);
    UITableView *table = objc_getAssociatedObject(btn, @"table");
}

是不是感觉OC也是很强大的。

 

OBJC_ASSOCIATION_ASSIGN = 0,           /**< Specifies a weak reference to the associated object. */
    OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, /**< Specifies a strong reference to the associated object. 
                                            *   The association is not made atomically. */
    OBJC_ASSOCIATION_COPY_NONATOMIC = 3,   /**< Specifies that the associated object is copied. 
                                            *   The association is not made atomically. */
    OBJC_ASSOCIATION_RETAIN = 01401,       /**< Specifies a strong reference to the associated object.
                                            *   The association is made atomically. */
    OBJC_ASSOCIATION_COPY = 01403          /**< Specifies that the associated object is copied.

这是关联的5中关联方式,简单说一下,很简单,看字面就能理解。第一个是弱引用关联对象,简称弱关联;第二个是(非原子访问)强关联,第三个是(非原子访问)复制对象关联;第四个是强关联,最后一个是复制对象关联;

 

转载于:https://www.cnblogs.com/pengoeng/p/8038405.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值