I want to put an 'i' button on the nav bar (on all the screens in which the nav bar appears). Touching on that button should bring up a view (perhaps modal) in which I can have controls. How do I put the button on the nav bar, and what how will I execute the call back?
+4
A:
Info Button Code:
UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:@selector(infoButtonAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *modalButton = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
[self.navigationItem setLeftBarButtonItem:modalButton animated:YES];
[modalButton release];
The code above calls method -(void)infoButtonAction. Add this to all of your ViewControllers or just create one .nib and use that for all of your views.
Jordan
2009-08-20 23:07:56
Perfect. I have two view controllers. Root and Detail. I added this code to both. I changed Left to Right. I also added:- (void) infoButtonAction { NSLog(@"Information");}It works fine. Each of my view controllers already have their own xib files as well. When you said "use that nib for all your views" what did you mean? I would love to have a simple view with static information show up. Sorry for the newb question. What is the simplest way to do that?
PrinceOfMaine
2009-08-21 06:08:45
How do I format the comment so that the code block shows up correctly as a code block.
PrinceOfMaine
2009-08-21 06:10:32
I was thinking that since you need this viewController for every view, you can create a viewController with the code above in the -viewDidLoad or -awakeFromNib method. Then subclass this viewController for each viewController you need. That way you only have the one place where the code for infoButton is. Just an idea.
Jordan
2009-08-21 13:13:29
That is an excellent idea.
PrinceOfMaine
2009-08-21 16:35:13