我有一个侧栏菜单,使用SWRevealViewController制作。
侧栏中的一个菜单元素是“登录”。它打开登录视图(通过自定义SWRevealViewController segue)。
我想在用户成功登录后以编程方式切换到另一个视图。我该怎么做?
更新,我的代码:
ViewController *vc = [[ViewController alloc] init]; // this is main front ViewController
SWRevealViewController *revealController = self.revealViewController;
[revealController setFrontViewController:vc animated:YES];
在此之后,我得到空屏幕。我究竟做错了什么?我觉得这是错的:
ViewController *vc = [[ViewController alloc] init];
那么访问已加载的视图控制器的正确方法是什么? (它在应用程序启动时加载SWRevealViewController作为前端控制器)
答案 0 :(得分:29)
试试这个:
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];
为我工作: - )
答案 1 :(得分:14)
使用
- (void)setFrontViewController:(UIViewController *)frontViewController animated:(BOOL)animated;
方法
SWRevealController有自己的属性,你可以从任何UIViewController访问:
SWRevealViewController *revealController = self.revealViewController;
您可以在它的GitHub存储库中检查示例项目: https://github.com/John-Lluch/SWRevealViewController
答案 2 :(得分:6)
这是setFrontViewController解决方案的Swift版本:
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)
}
答案 3 :(得分:5)
试试这个:
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];
答案 4 :(得分:5)
有一个新的API - (void)pushFrontViewController:(UIViewController *)frontViewController animated:(BOOL)animated;
据我所知,这是手动切换视图控制器之间正确动画的最优雅方式。
// 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];
如果您位于不导入SWRevealViewController
且想要推回的目标vc中:
// get your vc properly
[self.navigationController setViewControllers:@[vc] animated:YES];
请注意[self.navigationController showViewController:vc sender:self];
具有相同的结果,但不要使用它,因为这会在导航堆栈上创建一个新的视图控制器,这在使用SWRevealViewController
时基本上没有意义。
答案 5 :(得分:1)
@Evana有一个很好的答案,但是对于我将case参数传递给destViewController的情况,我需要改进它,如下所示。通过完全按照她的例子,我注意到DestViewController viewDidLoad被调用了两次。首先在' selectedId'上进行0,只有第二次调用才会选择' selectedId'正在接收我的标签值。
因此,为了更清晰的导航,必须从navController获取DestViewController,以便没有冗余调用。
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];