前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS按钮事件传參的二种方式

iOS按钮事件传參的二种方式

作者头像
码客说
发布2019-10-22 14:09:00
6080
发布2019-10-22 14:09:00
举报
文章被收录于专栏:码客

经典方式

添加变量

代码语言:javascript
复制
var buttonPars:[Int:IndexPath] = [:];

最常用的方式是直接给button设置tag

代码语言:javascript
复制
cell.actionButton.addTarget(self, action: #selector(actionButtonClick(button:)), for: UIControlEvents.touchUpInside)
let tagNum = indexPath.section*1000000 + indexPath.row;
cell.actionButton.tag = tagNum;
self.buttonPars[tagNum] = indexPath;

点击事件

代码语言:javascript
复制
@objc func actionButtonClick(button:UIButton){
    print("button.tag:\(button.tag)");
    if let indexPath = self.buttonPars[button.tag]{
        print("section:\(indexPath.section)");
        print("row:\(indexPath.row)");
    }
}

牛掰方式

iOS牛掰在可以修改运行时 可以直接绑定两个对象 具体代码如下

代码语言:javascript
复制
class SonghuoViewController: UIViewController{
	//定义静态变量
	static var action = "action";

	func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
	   let  cell = tableView.dequeueReusableCellWithIdentifier("songhuoCell", forIndexPath: indexPath) as! SonghuoTableViewCell;
	   cell.actionButton.addTarget(self, action: #selector(actionButtonClick(button:)), forControlEvents: UIControlEvents.TouchUpInside);
	   
	   //创建关联
	   objc_setAssociatedObject(cell.actionButton, &SonghuoViewController.action, indexPath, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC);
	   return cell;
	}
	
	@objc func actionButtonClick(button:UIButton){
		//获取关联的对象
	   if let indexPath = objc_getAssociatedObject(button, &SonghuoViewController.action) as? NSIndexPath{
	   	  print("section:\(indexPath.section)");
	   	  print("row:\(indexPath.row)");
	   }	   
	}
}

创建关联

代码语言:javascript
复制
objc_setAssociatedObject(cell.actionButton, &SonghuoViewController.action, indexPath, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC);

该函数需要四个参数:源对象,关键字,关联的对象和一个关联策略

  • 关键字是一个void类型的指针。每一个关联的关键字必须是唯一的。通常都是会采用静态变量来作为关键字。
  • 关联策略表明了相关的对象是通过赋值,保留引用还是复制的方式进行关联的;还有这种关联是原子的还是非原子的。 这里的关联策略和声明属性时的很类似。这种关联策略是通过使用预先定义好的常量来表示的。

获取关联对象

代码语言:javascript
复制
objc_getAssociatedObject(button, &SonghuoViewController.action)

该函数需要两个参数:源对象,关键字

删除关联

传入nil即可

代码语言:javascript
复制
objc_setAssociatedObject(cell.actionButton, &SonghuoViewController.action, nil, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC);
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-11-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 经典方式
  • 牛掰方式
    • 创建关联
      • 获取关联对象
        • 删除关联
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档