Sami Sami - 2 months ago 4
iOS Question

how to show back button on navigation bar?

I was practicing an application with navigation bar. I used UIBarButton to go to the next screen from the main screen.

UIButton *moveLeft = [UIButton buttonWithType:UIButtonTypeCustom];

[moveLeft setTitle:@"Next Screen" forState:UIControlStateNormal];
moveLeft.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0f];
[moveLeft.layer setCornerRadius:4.0f];
[moveLeft.layer setBorderWidth:1.0f];
[moveLeft.layer setBorderColor: [[UIColor redColor] CGColor]];
moveLeft.frame=CGRectMake(200, 100.0, 90.0, 30.0);
[moveLeft setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];

[moveLeft addTarget:self action:@selector(moveToNext) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem* barbutton= [[UIBarButtonItem alloc] initWithCustomView:moveLeft];

self.navigationItem.rightBarButtonItem = barbutton;

But now when the second screen appears there is no navigation bar.
Here are the screen shots of the app

main screen

Second Screen

i tried UIBarButton for back also but still navigation bar is not coming on the second screen.

UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"Back"
[[self navigationItem] setBackBarButtonItem:back];

My question how do i show the back button the second screen?? i am not making any custom button but the default is also not coming automatically.
Please guide me.


From your comments, you are presenting the next view controller, instead of this

[self presentViewController:secondScreen animated:YES completion:nil];   

Do like this

[self.navigationController pushViewController:secondScreen  animated:YES]  


Instead of adding subView to the window, you should set the rootViewController of the window which is the recommended way. So change

[self.window addSubview:[navControll view]];


self.window.rootViewController = navControll;