我想从一个视图控制器导航到另一个。如何将以下Objective-C代码转换为Swift?
UIViewController *viewController = [[self storyboard] instantiateViewControllerWithIdentifier:@"Identifier"];
UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.navigationController pushViewController:navi animated:YES];
发布于 2015-05-15 06:08:25
根据我的经验,navigationController
是空的,所以我把代码改成了:
let next = self.storyboard?.instantiateViewControllerWithIdentifier("DashboardController") as! DashboardController
self.presentViewController(next, animated: true, completion: nil)
不要忘记在StoryBoard
-> identity inspector
中设置ViewController StoryBoard Id
发布于 2015-12-15 01:15:38
如果您不希望出现后退按钮(这是我的情况,因为我想在用户登录后显示),下面是如何设置导航控制器根目录的方法:
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("YourViewController") as! YourViewController
let navigationController = UINavigationController(rootViewController: vc)
self.presentViewController(navigationController, animated: true, completion: nil)
发布于 2017-10-06 18:40:09
在swift 4.0中
var viewController: UIViewController? = storyboard().instantiateViewController(withIdentifier: "Identifier")
var navi = UINavigationController(rootViewController: viewController!)
navigationController?.pushViewController(navi, animated: true)
https://stackoverflow.com/questions/24038215
复制相似问题