How to set target and action for UIBarButtonItem at runtime
问题 Tried this but only works for UIButton: [btn setTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside]; 回答1: Just set the UIBarButtonItem's target and action properties directly. 回答2: UIBarButtonItem doesnt have the same addTarget method so you have to set them directly as follows btn.target = self; btn.action = @selector(barButtonCustomPressed:); ... // can specify UIBarButtonItem instead of id for this case -(IBAction)barButtonCustomPressed:(UIBarButtonItem*