xCode Navigation Back Button not popping detailview - ios

Whenever I press on the 'go back' button on my final detail view, it wont go back to the table view.
I have a storyboard setup like this:
UITabViewController -> UINavigationController -> UITableView -> UINavigationController -> UIView (The detailview).
When I run my program I see the back button, but clicking it does nothing.
Here is the code for DetailViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *backbtn = [[UIBarButtonItem alloc] initWithTitle:#"Go Back" style:UIBarButtonItemStylePlain target:self action:#selector(gobackBtn)];
[self.navigationItem setLeftBarButtonItem:backbtn];
[self configureView];
}
- (void)gobackBtn
{
[self.navigationController popViewControllerAnimated:YES];
}
As if [self.navigationController popViewControllerAnimated:YES]; does nothing.
I'm out of ideas. Why does the backbutton not 'pop' the detailview?

Your detail view controller is the root view controller of the navigation controller. So 'popping' does nothing. You need instead to dismiss the navigation controller. Technically, you should use a delegate to tell your TableViewController to dismiss it, but I think
[self.navigationController dismissViewControllerAnimated:YES completion:NULL];
from within your goBackButton method of your detail view controller will do the trick.

Related

How to back to rootViewController

How can I go back from 2nd view controller when I click on UIBarButtonItem using the custom segue?
UIBarButtonItem *backButton=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"back.png"] style:UIBarButtonItemStylePlain target:self action:#selector(back)];
I have used the following code for:
- (IBAction)back {
UIViewController *source=(UIViewController *)[self.storyboard instantiateViewControllerWithIdentifier:#"MainView"];
[self.navigationController popToViewController:source animated:YES];
}
instead of second line you can write:
[self.navigationController popToRootViewControllerAnimated:YES];
As other people say, you should use for your case:
[self.navigationController popToRootViewControllerAnimated:YES];
I want add that the method that you used, the popToViewController you should use it for to return to one specific view controller. In particular, this method pop the view controller on top of the navigation stack; you can check this and similar method for control the navigation in Apple Documentation.

Trying to handle the leftBarButton in UINavigationBar

My UINavigationBar is not handling properly.
I would like it to Navigate back to the last UIViewController, though i get a black screen when pressing the leftBAckButton and not the previous UIViewcontroller.
View
Controller.m viewDidLoad
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:#"&" style:UIBarButtonItemStyleBordered target:self action:#selector(home:)];
self.navigationItem.leftBarButtonItem = newBackButton;
This is the -(void)home:(UIBarButtonItem *)sender
-(void)home:(UIBarButtonItem *)sender {
[self.navigationController popToRootViewControllerAnimated:YES];
}
Though i feel this is correct i receive this when pressing the button
As you can see the navigationbar seems to disappear and it never navigates to the previous view controller.
When using [self.navigationController popViewControllerAnimated:YES];
I receive! this on the UI
What is causing this and how can i fix it?

Hiding a UINavigationBar after hitting back button

I have 3 view controllers and they are all chained together like so:
vc1 -> vc2 -> vc3
In view controller 1 and 2, I have the navigation bar hidden like so:
elf.navigationController.navigationBarHidden = NO;
The third view controller shows the navigation bar and has a back button on the left. When I hit the back button it goes to view controller 2 but the navigation bar is no longer hidden. How do I hide it again?
write this code in viewWillAppear method in viewController1 and viewController2
-(void)viewWillAppear:(BOOL)animated{
self.navigationController.navigationBarHidden = YES;
}
Thanks
Use this simple code in Third view controller
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}

not able to move to another view controller

I'm trying to move from 1st view controller -> 2nd view controller. However, it seems nothing is happening.
It is mainly from a sign-in page, so I don't need to get back to that page once I have a successful login. I'm trying right now from a button just to check.
I have 2 classes.
signInPage
optionsScreen
I need to go from 1 > 2
Inside signInPage.m
#import "optionsScreen.h"
- (IBAction)moveToNext:(id)sender {
optionsScreen *aSecondPageController =
[[optionsScreen alloc]
initWithNibName:#"optionsScreen"
bundle:nil];
[self.navigationController pushViewController:aSecondPageController animated:YES];
// [aSecondPageController release];
}
Screenshot of story board
https://drive.google.com/file/d/0B1y7ao4cGAYKaU0yRlVxcVg2bzA/edit?usp=sharing
Solution:
- (IBAction)moveToNext:(id)sender {
NSString * storyboardName = #"Main_iPhone";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"optionsScreen"];
[self presentViewController:vc animated:YES completion:nil];
}
In storyboard add navigation view controller. As root view controller - perhaps by default it is table view controller, so remove it - set up 1st view controller. Adding navigation controller you can push to navigation stack as many view controllers as you want.
I think what you're trying to do is present the view controller modally, not push it onto the stack (which doesn't exist). Instead you should just do this. This will not show a back button by default so you'll have to add one manually:
optionsScreen *aSecondPageController = [[optionsScreen alloc] initWithNibName:#"optionsScreen" bundle:nil];
[self presentViewController:aSecondPageController animated:YES completion:nil];
To go back, in your optionsScreen view controller, you'll need some sort of back button right? So you can do this in your viewDidLoad:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(backPressed:);
Then you'll have to create a new method called backPressed:
-(void)backPressed:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
Edit: it turns out you were trying to do what I explained above. I will leave the navigation controller bit for future reference.
If you are trying to use a navigation controller to push your options, which will give you a back button by default, then first, in your storyboard, click your sign in view controller, then at the menu bar at the top go to Editor > Embed In... > Navigation Controller. Then in your view controller switch method, you can do what you were trying before
optionsScreen *aSecondPageController = [[optionsScreen alloc] initWithNibName:#"optionsScreen" bundle:nil];
[self.navigationController pushViewController:aSecondPageController animated:YES];

Presenting a UINavigationController from a UIViewController doesn't have default cancel button

I am trying to present a UINavigationController modally from a UIViewController. From previous experience, I'm pretty sure that when I do this there should be a cancel button by default in the navigation bar, however with the following code the navigation bar is completely blank. Any ideas?
UIViewController *rootVC = [[UIViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootVC];
[self presentViewController:navController animated:YES completion:nil];
You will not get any bar button items by default. You need to either add the button before presenting, or, more correctly, add the button from inside the root view controller. Normally, you want to have delegation set up, where the presented view controller notifies the presenting view controller that it should dismiss it. Self-dismissing from inside the root view controller is usually not recommended.
– presentViewController:animated:completion: only going to present your view controller as a modal view controller, even you set up that as a navigation controller. the easiest solution in your case is in the navController set the left bar button as the cancel button, and call dismiss when click that button,
UIBarButtonItem *cancelButton= [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(dismissViewController)];
navController.navigationItem.leftBarButtonItem = cancelButton;
- (void)dismissViewController
{
[self dismissViewControllerAnimated:YES completion:nil];
}
Hope that would help.

Resources