ios SWRevealViewController - 手动切换到另一个视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21096326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SWRevealViewController - manually switch to another view?
提问by Roman
I have a sidebar menu, made with SWRevealViewController.
我有一个侧边栏菜单,用 SWRevealViewController 制作。
One of menu elements in sidebar is "sign-in". It opens sign-in view (via custom SWRevealViewController segue).
侧边栏中的菜单元素之一是“登录”。它打开登录视图(通过自定义 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)
那么访问已加载的视图控制器的正确方法是什么?(它在应用程序启动时由 SWRevealViewController 作为前端控制器加载)
采纳答案by vburojevic
Use
- (void)setFrontViewController:(UIViewController *)frontViewController animated:(BOOL)animated;
method
使用
- (void)setFrontViewController:(UIViewController *)frontViewController animated:(BOOL)animated;
方法
SWRevealController has it's own property you can access from any UIViewController:
SWRevealController 有它自己的属性,你可以从任何 UIViewController 访问:
SWRevealViewController *revealController = self.revealViewController;
You can check example projects at it's GitHub repository: https://github.com/John-Lluch/SWRevealViewController
您可以在其 GitHub 存储库中查看示例项目:https: //github.com/John-Lluch/SWRevealViewController
回答by
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 :-)
为我工作:-)
回答by Marc Glassman
Here is a Swift version of the setFrontViewController solution:
这是 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)
}
回答by benaneesh
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];
回答by Brian
There is a new API - (void)pushFrontViewController:(UIViewController *)frontViewController animated:(BOOL)animated;
有一个新的 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:
如果您在未导入的目标 vc 中SWRevealViewController
并想要推回:
// 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
.
请注意它[self.navigationController showViewController:vc sender:self];
具有相同的结果,但不要使用它,因为这会在导航堆栈上创建一个新的视图控制器,这在使用SWRevealViewController
.
回答by Evana
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];
回答by teapeng
@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.
@Evana 有一个很好的答案,但是对于我将参数传递给 destViewController 的情况,我需要如下改进它。通过严格按照她的示例,我注意到 DestViewController viewDidLoad 被调用了两次。首先在“selectedId”上为 0,只有第二次调用时“selectedId”才接收我的标签值。
Thus, in order for a cleaner navigation, the DestViewController must be obtained from the navController, so that there is no redundant call.
因此,为了更清晰的导航,必须从 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];