Roman
Roman

Reputation: 3843

SWRevealViewController - manually switch to another view?

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)

Upvotes: 21

Views: 36066

Answers (7)

TPG
TPG

Reputation: 3211

@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];

Upvotes: 1

Marc Glassman
Marc Glassman

Reputation: 101

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)
} 

Upvotes: 6

Evana
Evana

Reputation: 1794

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];

Upvotes: 1

Brian
Brian

Reputation: 31282

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.

Upvotes: 5

user3963829
user3963829

Reputation:

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 :-)

Upvotes: 31

benaneesh
benaneesh

Reputation: 416

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];

Upvotes: 5

vburojevic
vburojevic

Reputation: 1666

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

Upvotes: 16

Related Questions