1.通过界面获取控件并且添加点击的事件
2.通过代码设置事件处理方法
总结下第二种方法:-addTarget:action:forControlEvent
addTarget:参数:指定任意对象
action参数:SEL参数
forControlEvent参数:指定事件类型
如果要为控件添加特定的事件,第一需要获取控件:tag
[UIButton setTag : 1];
比如:UIButton【[self.loginBtn addTarget:self action:@selector(loginHandler:)forConrolEvents:UIControlEventouchUpInside];】
-(void)loginHandler:(UIButton*)sender
{
NSSlog = @" 点击了我!"
}
3.对于lable则必须使用委托对象来处理事件:
UITapGestureRecognizer *tapRecognizerWeibo=[[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(openURL:)];
self . weiboLabel . userInteractionEnabled = YES ;[self.weiboLabel addGestureRecognizer:tapRecognizerWeibo];