创建一个UIButton 并用Target - Action来监听它的点击事件
Target -- self控制器
Action -- 具体动作,self控制器中的某个方法
forControlEvents:UIControlEventTouchUpInside -- 表示监听的事件
1 - (void)btnclick:(id)sender 2 { 3 NSLog(@"点击%@" , [sender class]); 4 } 5 6 - (void)viewDidLoad { 7 [super viewDidLoad]; 8 // Do any additional setup after loading the view, typically from a nib. 9 10 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 11 btn.frame = CGRectMake(100, 100, 100, 100); 12 [btn setTitle:@"点击" forState:UIControlStateNormal]; 13 14 [btn addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside]; 15 16 [self.view addSubview:btn]; 17 }