21

I have a sidebar menu, made with SWRevealViewController.

One of menu elements in sidebar is "sign-in". It opens sign-in view (via custom SWRevealViewController segue).

I want to programmatically switch to another view after user successfully signs in. How do I do that?

UPDATE, my code:

ViewController *vc = [[ViewController alloc] init]; // this is main front ViewController
SWRevealViewController *revealController = self.revealViewController;
[revealController setFrontViewController:vc animated:YES];

After this I get just empty screen. What am I doing wrong? I feel that this is wrong:

ViewController *vc = [[ViewController alloc] init];

so what is correct way to access already loaded view controller? (it loads at app start by SWRevealViewController as front controller)

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Roman
  • 3,799
  • 4
  • 30
  • 41

7 Answers7

31

Try this:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
SecondViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[navController setViewControllers: @[rootViewController] animated: YES];

[self.revealViewController setFrontViewController:navController];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];

work for me :-)

Brian
  • 30,156
  • 15
  • 86
  • 87
  • 1
    As for someone who has different storyboards, this is how my code looks like (referenced from the code above): UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"OrderHistory" bundle:nil]; UINavigationController *navController = [storyboard instantiateInitialViewController]; [self.revealViewController setFrontViewController:navController]; [self.revealViewController setFrontViewPosition:FrontViewPositionLeft animated: YES]; – JCurativo Apr 01 '15 at 06:05
  • on the subview of frontview every time call method of mainviewcontroller method even there is no any navigation between them can anyone one help me? :( – Rushang Prajapati Aug 21 '15 at 05:47
16

Use - (void)setFrontViewController:(UIViewController *)frontViewController animated:(BOOL)animated; method

SWRevealController has it's own property you can access from any UIViewController:

SWRevealViewController *revealController = self.revealViewController;

You can check example projects at it's GitHub repository: https://github.com/John-Lluch/SWRevealViewController

vburojevic
  • 1,666
  • 5
  • 24
  • 34
  • thanks! but could you please explain how can I access this method from other class? (I'm new to iOS) I already have `#import "SWRevealViewController.h"` - but how can I get current instance of SWRevealViewController to call its method? – Roman Jan 13 '14 at 16:51
  • Where do you create the mentioned controller at startup? – vburojevic Jan 13 '14 at 17:18
  • You can look at the example projects at libraries GitHub page, I have updated my answer – vburojevic Jan 13 '14 at 17:22
  • Could you please help me bro @vburojevic ? http://stackoverflow.com/questions/42573338/swrevealviewcontroller-button-not-work-after-clicking-back-button-from-another-v . I tired it last 2 days. I still did not get the solution – May Phyu Mar 07 '17 at 06:35
6

Here is a Swift version of the setFrontViewController solution:

if let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("SecondViewControllerStoryBoardID") as? SecondViewController {
  let navController = UINavigationController(rootViewController: secondViewController)
  navController.setViewControllers([secondViewController], animated:true)
  self.revealViewController().setFrontViewController(navController, animated: true)
} 
Marc Glassman
  • 101
  • 1
  • 4
5

Try this:

ViewController *vc = [[ViewController alloc] init]; // this is main front ViewController
UINavigationController* navController = (UINavigationController*)self.revealViewController.frontViewController;
[navController setViewControllers: @[vc] animated: YES];
// [navController pushViewController:vc animated:YES]; // use this if you want to stack the views
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
benaneesh
  • 426
  • 6
  • 10
  • can u tell me how to set SWREvealview controller as Root view controlelr via code... pls – iOSDeveloper Dec 11 '14 at 07:17
  • I haven't implemented it programmatically, but it should something like this: Use `- (id)initWithRearViewController:(UIViewController *)rearViewController frontViewController:(UIViewController *)frontViewController;` and the "MenuViewController" as the rearViewController and the NavigationController as the frontViewController. Hope this helps – benaneesh Dec 11 '14 at 18:32
  • @benaneesh I am moving to a view controller via presentViewController(). And in viewDidLoad(),I am implementing this method but always, I am getting a nill value in my viewcontroller at self.revealViewController(). Any Idea?? – G.Abhisek Feb 17 '16 at 05:25
  • presentViewController() doesn't use the current navigationcontroller and thats your problem there. Either do not present it modally or figure out how take the current navigation controller and present that modally with the new viewcontroller as the frontviewcontroller. – benaneesh Feb 18 '16 at 03:19
5

There is a new API - (void)pushFrontViewController:(UIViewController *)frontViewController animated:(BOOL)animated;

As far as I know, this is the most elegant way that animates correctly to switch between view controllers manually.

// get the view controller you want to push to properly.
// in this case, I get it from Main.storyboard
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController * vc = [sb instantiateViewControllerWithIdentifier:@"SignInViewController"];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc];

[navController setViewControllers: @[vc] animated:NO];
[self.revealViewController pushFrontViewController:navController animated:YES];

If you are in the target vc that doesn't import SWRevealViewController and want to push back:

// get your vc properly
[self.navigationController setViewControllers:@[vc] animated:YES];

Please notice that [self.navigationController showViewController:vc sender:self]; has the same result, but don't use this since this creates a new view controller on the navigation stack, which is basically meaningless while using SWRevealViewController.

Brian
  • 30,156
  • 15
  • 86
  • 87
  • Hello @Brian I need your help! – Rushang Prajapati Aug 21 '15 at 05:30
  • Please check out this my question http://stackoverflow.com/questions/32115308/swrevealviewcontroller-push-from-subview – Rushang Prajapati Aug 21 '15 at 05:48
  • hello bro @Brian, could you please check my link http://stackoverflow.com/questions/42573338/swrevealviewcontroller-button-not-work-after-clicking-back-button-from-another-v ? I also tried almost 1 week :( . I don't know how to solve this. :( – May Phyu Mar 07 '17 at 09:39
1

This one works for me perfectly

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];

UINavigationController *navController = [storyboard instantiateViewControllerWithIdentifier:@"NavController"];
[navController setViewControllers: @[rootViewController] animated: YES];

[self.revealViewController setFrontViewController:navController];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
Evana
  • 1,754
  • 13
  • 12
  • I am moving to a view controller via presentViewController(). And in viewDidLoad(),I am implementing this method but always, I am getting a nill value in my viewcontroller at self.revealViewController(). Any Idea?? – G.Abhisek Feb 17 '16 at 05:25
  • Can you please have a look over this question of mine http://stackoverflow.com/q/35699581/5395919 . I am quite struggling on this issue. – G.Abhisek Feb 29 '16 at 12:38
1

@Evana has a good answer, however for my case which passing parameter to the destViewController, I need to improve it as below. By following her example exactly, I noticed the DestViewController viewDidLoad was called twice. First with 0 on the 'selectedId', only the second call did the 'selectedId' is receiving my tag value.

Thus, in order for a cleaner navigation, the DestViewController must be obtained from the navController, so that there is no redundant call.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

UINavigationController *navController = [storyboard instantiateViewControllerWithIdentifier:@"DestNavController"];

DestViewController *destViewController = (DestViewController*)[[navController viewControllers] objectAtIndex:0];

destViewController.selectedId = ((UIButton*)sender).tag;

[self.revealViewController setFrontViewController:navController];
TPG
  • 2,811
  • 1
  • 31
  • 52
  • I have a problem and I don't know how to solve it. Could you please check this bro @teapeng ? http://stackoverflow.com/questions/42573338/swrevealviewcontroller-button-not-work-after-clicking-back-button-from-another-v – May Phyu Mar 07 '17 at 09:38