adrian
adrian

Reputation: 4594

navigationController issue

I have the following:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath(NSIndexPath*)indexPath
{

     audiChassisInputViewController = [[myAudiChassisInputViewController alloc] init];    

    [self.navigationController pushViewController:audiChassisInputViewController animated:YES];

    self.navigationController.navigationBarHidden = NO;

    UIBarButtonItem *retourButton = [[UIBarButtonItem alloc] initWithTitle:@"Retour" style:UIBarButtonItemStyleBordered target:self.navigationController action:@selector(popViewControllerAnimated:)]; 
    [self.navigationController.navigationBar.topItem setLeftBarButtonItem:retourButton];
    [self.navigationController.navigationBar.topItem setTitle:@"Chassis Input"];
    [retourButton release];

    [audiChassisInputViewController release];

}

and this workes...the new view is showed.

in the new view:

myAudiChassisInputViewController.h

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    chassisInputTextView.layer.cornerRadius = 15;
    chassisInputTextView.clipsToBounds = YES;
    [chassisInputTextView becomeFirstResponder];

    UIBarButtonItem *okButton = [[UIBarButtonItem alloc] initWithTitle:@"OK" style:UIBarButtonItemStyleBordered target:self action:@selector(chassisOkPressed)];  
    [self.navigationController.navigationBar.topItem setRightBarButtonItem:okButton];
    [okButton release];

}

I have no error, but there is no right bar button shown.Anyone, any idea why?

Upvotes: 0

Views: 742

Answers (3)

albertamg
albertamg

Reputation: 28572

Change this line:

[self.navigationController.navigationBar.topItem setRightBarButtonItem:okButton];

with this line:

[[self navigationItem] setRightBarButtonItem:okButton];

The thing is, by the time viewDidLoad is executed, the top item of the navigation bar (self.navigationController.navigationBar.topItem) is still pointing to the navigation item of the back view controller.

The back view controller is the one that used to be the top view controller before the current top view controller was pushed onto the stack ([[viewControllers objectAtIndex:[viewControllers count] - 2] navigationItem]). The following snippet shows how the top item of the navigation bar is still pointing to the navigation item of the back view controller in viewDidLoad and it is for illustration purposes only:

// the view controllers currently on the navigation stack
NSArray *viewControllers = self.navigationController.viewControllers;
// The root view controller is at index 0 in the array, the back view controller is at index n-2, and the top controller is at index n-1, where n is the number of items in the array.
UIViewController *backViewController = [viewControllers objectAtIndex:[viewControllers count] - 2];
// get the navigation item of the back view controller
UINavigationItem *backNavigationItem = backViewController.navigationItem;
UINavigationItem *topItem = self.navigationController.navigationBar.topItem;
if  (backNavigationItem == topItem) {
    NSLog(@"This gets logged to the console");
}

Upvotes: 2

Praveen-K
Praveen-K

Reputation: 3401

If you have xib file of your class, then add the navigation controller and add the navigation bar and under that add the UIBarButton.

Upvotes: 0

sagarkothari
sagarkothari

Reputation: 24810

Go to your

myAudiChassisInputViewController.m file

place following code

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIBarButtonItem *retourButton = [[UIBarButtonItem alloc] initWithTitle:@"Retour" style:UIBarButtonItemStyleBordered target:self.navigationController action:@selector(popViewControllerAnimated:)];

    UIBarButtonItem *itemOkey=[[UIBarButtonItem alloc] initWithTitle:@"OK" style:UIBarButtonItemStyleBordered target:self action:@selector(chassisOkPressed)];

    self.navigationItem.rightBarButtonItem=itemOkey;
    self.navigationItem.leftBarButtonItem=retourButton;
}

I have the valid output as follows that you want to have enter image description here

Hope it helps to you.

Upvotes: 1

Related Questions