0

My iOS application is based on John-Lluch's SWRevealViewController to get sidebar navigation. I have ran into a navigation problem that I don't know how to solve.

I want one of the menu items to lead to a page that doesn't contain a sidebar but a back button instead. (The other menu items leads to pages where the sidebar can be opened).

To get a back button on a view I need to use a Show Detail Segue, but I also need a navigation controller "somewhere" for the back button to show up. (I don't mean that I need a navigation controller AFTER the segue - I have that already - but I need it somewhere before).

Have someone used SWRevealViewController in this way or know how to achieve this? "Where" should I place a navigation controller?

Thanks!

Joakim
  • 3,224
  • 3
  • 29
  • 53
  • I have a similar issue. I want to be able to go back to the view controller I was on before I triggered the navigation via the side menu. I have not managed to find an answer yet. Did you? – zevij Oct 28 '15 at 18:37
  • I forgot to post my solution to this. It's now an answer so you can check it out. I'm not sure I'm 100 % happy with it, but it was the only solution I found. – Joakim Oct 29 '15 at 08:18

1 Answers1

0

The only working solution I came up with was to use a Category on UIView, that could do my Detail Segues.

@interface UIViewController (SidebarView)

  • (void)setupSidebarWithGestureRecognizer:(BOOL)useGestureRecognizer;
  • (void)openSettingsPage;
  • (void)openAboutPage;

@end

and the .m-file's code for openAboutPage:

/* This method is called by the sidebar. If the sidebar opens the settings page from itself, the back button
 * doesn't know where it should point. This method opens the about menu when the sidebar tells it to, and the back button
 * is pointing to the home page. */
- (void)openAboutPage {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *about = [storyboard instantiateViewControllerWithIdentifier:@"About"];
    [self.revealViewController rightRevealToggleAnimated:YES];
    [self.navigationController pushViewController:about animated:NO];
    self.navigationController.navigationItem.title = @"About";
}

and the setupSideBarWithGestureRecognizer... method

- (void)setupSidebarWithGestureRecognizer:(BOOL)useGestureRecognizer {
    SWRevealViewController *revealViewController = self.revealViewController;
    if (revealViewController) {
        UIBarButtonItem *sidebarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"reveal"] landscapeImagePhone:[UIImage imageNamed:@"reveal"] style:UIBarButtonItemStylePlain target:self.revealViewController action:@selector(rightRevealToggle:)];
        self.navigationItem.rightBarButtonItem = sidebarButton;
        if(useGestureRecognizer) {
            [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
        }
    }
}

Then for each page I want to show a sidebar, I just call [self setupSidebar:YES]; in the viewWillAppear method of that view controller.

Joakim
  • 3,224
  • 3
  • 29
  • 53
  • OK. So my problem is different. I was able to add the sidebar to any view controller but my issue is the back button. For example, if I'm on vc1, open sidebar and go to vc2, I want (in 1-2 cases) to be able to navigate to vc1 via a back button. The only solution I have is create an unwind/segue instead of a real back button as the views do not share a nav controller. – zevij Oct 29 '15 at 08:35
  • Yes that is exactly the problem that I solved. If I am in one page in my app and go to my settings page from the sidebar, I get a back button that takes me to the page I was before. Might be worth mentioning that I have put a navigation controller in front of every page in my sidebar as well (that is the navigation controller that get's the opened page added to itself. – Joakim Oct 29 '15 at 08:45
  • OK. So you're saying that your code basically does the following: you're in vc1. You open the sidebar and select vc2. Then in vc2 (not the sidebar you open from vc2) you get a back button to vc1? – zevij Oct 29 '15 at 08:49
  • 1
    OK. In that case I better take a 2nd look and convert to Swift and see if it actually duffers from what I've done. Thanks! – zevij Oct 29 '15 at 09:09