Sami
Sami

Reputation: 139

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.

Upvotes: 5

Views: 6718

Answers (4)

Haijing He
Haijing He

Reputation: 9

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"

Upvotes: 1

Bhanu
Bhanu

Reputation: 1249

Simply u can add custom back button to navigationBar

UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, button_width, button_height)];
[backButton setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

Upvotes: 0

Anil Varghese
Anil Varghese

Reputation: 42977

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;

Upvotes: 2

kaspartus
kaspartus

Reputation: 1365

You have to use UINavigationController. It will be root view controller for your app. Put mainVC into it and then just pushViewController second vc.

You can access navigation controller via navigationController method of any vc(mainVC in your case).

Upvotes: 0

Related Questions