Note: I’ve only tested this on the simulator.
I want to add the target-action in code instead of connecting it as an IBAction in Interface Builder.
theButton is on the navigation bar at the top left, created in IB.
Steps:
-
Declared
theButtonas an IBOutlet and connected it in IB. -
Added this in
viewDidLoad:self.theButton.target = self; self.theButton.action = @selector(theAction); -
I’m testing
theActionby this:- (void)theAction { NSLog(@"theAction called"); //do some other stuff }
When I click on theButton in the simulator, nothing happens. I don’t see the NSLog statement at all.
What am I missing?
Figured it out. Had to do with my configuration. I’m using a tab bar controller that uses the same view controller for multiple tabs. Each view controller shows different filtered data for some tab items.
Therefore, in such a configuration, you must be sure to connect the IBOutlets for each view controller that’s contained by a tab bar item. I only connected one, which is why it wasn’t working for some of the tab items.