Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"self.navigationItem.rightBarButtonItem" Not working

I have the following code taken straight from the NavBar sample code from Apple. I put this in the viewDidLoad method for a view in my app that is being presented modally, and it wont work.

UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"AddTitle", @"")
                                                               style:UIBarButtonItemStyleBordered
                                                              target:self
                                                              action:@selector(addAction:)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;

Any Suggestions?

like image 300
tarheel Avatar asked Apr 27 '26 01:04

tarheel


2 Answers

Okay explained solution:

presentModalViewController:animated: presents a viewController modally, which does not have a UINavigationBar, so you can do some things:

  1. Add a UINavigationBar in your viewController's nib and add the "Add" button there and everything you need to setup.
  2. You can use pushViewController:animated: to show the viewController modally which will be on the navigation stack and have the UINavigationBar for you to add your button
  3. If your first viewController is not a UINavigationController, using pushViewController:animated: won't solve it, so you can present a UINavigationController modally with your viewController as the rootViewController:
 YourViewController *viewController =[[[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil] autorelease];
    UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:viewController] autorelease];
    [self.navigationController presentModalViewController:navController animated:YES];

Hope any of this helps

like image 63
Herz Rod Avatar answered Apr 30 '26 13:04

Herz Rod


You need to use these lines of code on the page where you present the other view.

sceondController *obj=[[[sceondController alloc] initWithNibName:@"sceondController" bundle:nil] autorelease];
        UINavigationController *navController=[[[UINavigationController alloc] initWithRootViewController:obj] autorelease];

        [self.navigationController presentModalViewController:navController animated:NO];

and in second view use same code which you are using for making navigation button.

May be it resolves your problem.

like image 31
Ishu Avatar answered Apr 30 '26 12:04

Ishu