Ive working on an ios app and i am currently trying to add custom buttons to my navbar.
UIBarButtonItem *optButton = [[UIBarButtonItem alloc]
initWithTitle:@"Option1"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(Option:)];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:title];
item.leftBarButtonItem = optButton;
[navBar pushNavigationItem:item animated:YES];
[optButton release];
[item release];
-(void)Option:(id)sender{
NSLog(@"****Look at me!*****");
}
Now this works in that it displays the button on my navbar on the left as hoped but nothing happens when i click on the button. it doesnt react to touch at all. How do i get the custom button to react to user input? the navbar is created in code and im not using a nav controller or IB at all. I have the navbars delegate set with
[ navBar setDelegate: self ];
within the view controller that owns it. any help would be appreciated
edit: this is how i create the navbar in viewdidload
navBar = [ [ UINavigationBar alloc ] initWithFrame: CGRectMake(0, 0, 320, 45.0f)];
[ navBar setDelegate: self ];
[navBar pushNavigationItem:[ [ UINavigationItem alloc ] initWithTitle:@"MoM" ] animated:YES];
[ self.view addSubview: navBar ];
the reason for not using a nav controller for this is that i want to be able to change properties of the navbar and according to apples references on nav controllers i shouldnt amend the frame of a nav controllers nav bar directly. If im misunderstanding this please say so. I dont need a nav controller in this view controller as it will be displayed at all times in the app. I have separate nav controllers that handle transition of other view controllers separately but this particular view controller is a stand alone entity that is always available at top of the screen no matter what is going on else where
Discussion
It is permissible to modify the barStyle or translucent properties of
the navigation bar but you must never change its frame, bounds, or
alpha values directly. To show or hide the navigation bar, you should
always do so through the navigation controller by changing its
navigationBarHidden property or calling the
setNavigationBarHidden:animated: method.
edit2:
after some more research i see people have had their apps rejected from messing with the navbar frame properties within a nav controller so im def not gonna try this. I see on apples site that they do allow a stand alone navbar which i can mess with its properties and have the view controller take the uinavbar delegate. making the view controller have to handle all the input through the navbar directly. this looks to be the approach i want
If you are using a navigation bar as a standalone object, you should
assign a custom delegate object to the delegate property and use that
object to intercept messages coming from the navigation bar. Delegate
objects must conform to the UINavigationBarDelegate protocol. The
delegate notifications let you track when navigation items are pushed
or popped from the stack. You would use these notifications to update
the rest of your application’s user interface.
Set the current view controller’s
navigationItem‘sleftBarButtonItemproperty instead of creating one and pushing..