2

I am trying to programmatically show a view that I already have coded up, and give it a back button so that the user can easily pop back to my current view. Here is the only code I can get working, but it does not show a back button!

UIStoryboard *storyboard = self.storyboard;
UIViewController *root = [[[UIApplication sharedApplication] keyWindow] rootViewController];

UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"Privacy"];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];

navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;

[root presentViewController:navigationController  animated:YES completion:nil];

I've tried initializing the navigationController with the "root" viewController in the code above, then pushing the "controller" view controller via pushViewController. This crashes when I try to init with the "root" with a redacted stack trace.

How can I programmatically open a view with a back button to the current view?

I've looked everywhere on stackoverflow and this nuanced scenario is not addressed. Please help!

TheJeff
  • 3,665
  • 34
  • 52

4 Answers4

3
  1. You should not use presentViewController:, but instead use pushViewController:
  2. You should push viewController that you want to present, from viewController which is currently on screen, and which has navigationController.
  3. Probably your rootViewController for UIWindow object should be the navigationController, and rootViewController for that navigationController should be viewController from which you want to push.
Pavel Gatilov
  • 2,570
  • 2
  • 18
  • 31
  • My only issue with this is that your answer requires my current view to be within a navigation view controller. #2 is not in our design. I do not need a navigation controller for the view that I'm on, however a navigation controller is required for the view that want to present so that they can go back to the original page. – TheJeff Aug 29 '16 at 17:27
0

To add Navigation controller first you have to put this in your AppDelegate.m

UIStoryboard *sb=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc=[sb instantiateViewControllerWithIdentifier:@"View1"];

UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController=nav;

than if you want to push view's when you clicked on a putton or something you can achieve that by:-

[self.navigationController pushViewController:ViewName animated"YES];

and if you want to return from that view you can simply achieve that by popping the View(In Case your back Button):-

[self.navigationController popViewControllerAnimated:YES];
tryKuldeepTanwar
  • 3,490
  • 2
  • 19
  • 49
  • The problem I have with this is that I do not want the view that I'm on to be within a navigation controller. I just need to add a navigation controller to the view I am changing to, so that they can click the back button to go back to the conroller that does not have a nav bar. – TheJeff Aug 29 '16 at 17:24
0

Your code is right, just create custom back button in your PrivacyViewController

In viewDidLoad

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(goPrevious)];

And method is like :

- (void)goPrevious {

    [self dismissViewControllerAnimated:TRUE completion:^{

    }];
}
VRAwesome
  • 4,721
  • 5
  • 27
  • 52
0

By far the easiest thing to do here is to embed the view I am using into a navigation controller. In the Interface builder, click on the view, then go to Editor -> Embed -> Navigation Controller.

From here, it is easy to hide the navigation bar itself in -(void)viewWillAppear:animated:

- (void)viewWillAppear:(BOOL)animated {
    //Hide the nav bar for the onboarding screens.
    [[self navigationController] setNavigationBarHidden:YES animated:YES];
}

Then here is the code to segue to another page with an automatically populated back button:

UIStoryboard *storyboard = self.storyboard;
UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:identifier];
[[self navigationController] pushViewController:controller animated:YES];
[[self navigationController] setNavigationBarHidden:NO animated:YES];
TheJeff
  • 3,665
  • 34
  • 52