Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
                                                             style:UIBarButtonItemStylePlain
                                                            target:nil
                                                            action:nil];
    [[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.

like image 575
Sami Avatar asked Oct 21 '22 11:10

Sami


2 Answers

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]  

Suggestion

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]];

To

self.window.rootViewController = navControll;
like image 144
Anil Varghese Avatar answered Oct 23 '22 02:10

Anil Varghese


No one answered the question correctly, I met the same question, and I have solve it by myself. My solution is delete the navigation controller before the child view controller , than control drag between the tableview controller to the child view controller, select the "show"

like image 25
Haijing He Avatar answered Oct 23 '22 03:10

Haijing He