I created a UINavigationBar programmatically, but it is not displayed, what to do so that it is displayed with the UIBarButtonSystemItemUndo style button?

- (void)viewDidLoad { nnavBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0.0, 20.0, 320.0, 44.0)]; nnavBar.barStyle = UIBarStyleBlack; nnavBar.tintColor = [UIColor colorWithRed:0.6f green:0.0f blue:0.0f alpha:1.0]; UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemUndo target:nil action:@selector(doneInfoView)]; self.navigationItem.leftBarButtonItem = backButton; } -(void) doneInfoView { [self dismissModalViewControllerAnimated:YES]; 

}

    1 answer 1

    I see 2 errors here:

    1. You created NavigationBar, but how can an application understand where you want to place it? :) You did not add it.
    2. The same with the Button, how can the application understand that you put it in the created bar? :)

    Answer:

     - (void)viewDidLoad { nnavBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0.0, 20.0, 320.0, 44.0)]; nnavBar.barStyle = UIBarStyleBlack; nnavBar.tintColor = [UIColor colorWithRed:0.6f green:0.0f blue:0.0f alpha:1.0]; UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemUndo target:nil action:@selector(doneInfoView)]; UINavigationItem * navItem = [[UINavigationItem alloc] initWithTitle:@"Title"]; navItem.leftBarButtonItem = backButton; [nnavBar pushNavigationItem:navItem animated:NO]; [self.view addSubview:nnavBar]; }