i tried to define a UIBarButtonItem programatically. it showed up in the navBar as expected. but it didn´t call the action until i went to the IB and dragged an bar button item into the navigation controller object and assigned it with the didTapSearch action.
i thought it´s possible to define UIBarButtonItems programatically only, without any additional IB work. or is that only possible if the navigation controller and/or the view controller was created programatically as well?
or did i miss to set a delegate in my code again?
UIBarButtonItem *searchBtn = [[[UIB开发者_JAVA百科arButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(didTapSearch:)] autorelease];
self.navigationItem.rightBarButtonItem = searchBtn;
try this way instead in the header, declare the barButtonItem
proximityButton= [[UIBarButtonItem alloc]
initWithTitle:@"Nearby Locations"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(addToProximity)];
self.navigationItem.leftBarButtonItem = proximityButton;
and you can take out the :(id)sender part of the action
You could specific the action:
method programatically as you have done with your UIBarButtonItem
.
Implement action method in the class of object which you specified with target:
.
-(void) didTapSearch:(id) sender
{
UIBarButtonItem *searchBtn = (UIBarButtonItem*)sender;
}
Add a color to it.
searchBtn.tintColor = [UIColor blackColor];
精彩评论