Search code examples
iosipadxcode4uinavigationcontroller

Navigation Controller back button


How can I create a back button of a navigation controller programmatically?


Solution

  • In -(void)loadView or similar:

    UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:self action:@selector(backPressed:)];
    self.navigationItem.leftBarButtonItem = btn;
    [btn release];
    
    -(void)backPressed: (id)sender
    {
        [self.navigationController popViewControllerAnimated: YES]; // or popToRoot... if required.
    }