如何使用Swift从一个View Controller导航到另一个

sat*_*ish 76 uinavigationcontroller viewcontroller ios uinavigation swift

我想从一个视图控制器导航到另一个视图控制器.如何将以下Objective-C代码转换为Swift?

UIViewController *viewController = [[self storyboard] instantiateViewControllerWithIdentifier:@"Identifier"];
UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.navigationController pushViewController:navi animated:YES];
Run Code Online (Sandbox Code Playgroud)

小智 188

为第二个视图控制器创建一个swift文件(SecondViewController.swift),并在相应的函数中输入:

let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController
self.navigationController.pushViewController(secondViewController, animated: true)
Run Code Online (Sandbox Code Playgroud)


Swift 2+

let mapViewControllerObj = self.storyboard?.instantiateViewControllerWithIdentifier("MapViewControllerIdentifier") as? MapViewController
self.navigationController?.pushViewController(mapViewControllerObj!, animated: true)
Run Code Online (Sandbox Code Playgroud)

斯威夫特4

let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "IKDetailVC") as? IKDetailVC
self.navigationController?.pushViewController(vc!, animated: true)
Run Code Online (Sandbox Code Playgroud)

  • 我也有同样的问题,我被困在如何从一个ViewController导航到另一个.当我尝试这段代码时,我收到了这样的错误:`在代码的第二行展开一个Optional值时意外地找到了nil.请帮忙 (3认同)
  • 您需要确保将控制器嵌入到NavigationController中,才能正常工作,否则会出现错误。 (2认同)

Moj*_*bye 33

在我的经验navigationController是零,所以我将我的代码更改为:

let next = self.storyboard?.instantiateViewControllerWithIdentifier("DashboardController") as! DashboardController
self.presentViewController(next, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

不要忘记StoryBoard IdStoryBoard- >中设置ViewControlleridentity inspector

  • 这是因为您的视图控制器没有包含在导航视图控制器中。 (3认同)

Kei*_*day 16

如果您不希望显示后退按钮(这是我的情况,因为我想在用户登录后显示),这里是如何设置导航控制器的根目录:

let vc = self.storyboard?.instantiateViewControllerWithIdentifier("YourViewController") as! YourViewController
        let navigationController = UINavigationController(rootViewController: vc)
        self.presentViewController(navigationController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)


Mak*_*zev 15

SWIFT 3.01

let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "Conversation_VC") as! Conversation_VC
self.navigationController?.pushViewController(secondViewController, animated: true)
Run Code Online (Sandbox Code Playgroud)


Pro*_*col 6

在swift 4.0中

var viewController: UIViewController? = storyboard().instantiateViewController(withIdentifier: "Identifier")
var navi = UINavigationController(rootViewController: viewController!)
navigationController?.pushViewController(navi, animated: true)
Run Code Online (Sandbox Code Playgroud)


iOS*_*iOS 6

在 Swift 4.1 和 Xcode 10 中

这里AddFileViewController是第二个视图控制器。

故事板 ID 为 AFVC

let next = self.storyboard?.instantiateViewController(withIdentifier: "AFVC") as! AddFileViewController
self.present(next, animated: true, completion: nil)

//OR

//If your VC is DashboardViewController
let dashboard = self.storyboard?.instantiateViewController(withIdentifier: "DBVC") as! DashboardViewController
self.navigationController?.pushViewController(dashboard, animated: true)
Run Code Online (Sandbox Code Playgroud)

如果需要,请使用线程。

前任:

DispatchQueue.main.async { 
    let next = self.storyboard?.instantiateViewController(withIdentifier: "AFVC") as! AddFileViewController
    self.present(next, animated: true, completion: nil) 
}
Run Code Online (Sandbox Code Playgroud)

如果你想在一段时间后搬家。

前任:

//To call or execute function after some time(After 5 sec)
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
    let next = self.storyboard?.instantiateViewController(withIdentifier: "AFVC") as! AddFileViewController
    self.present(next, animated: true, completion: nil) 
} 
Run Code Online (Sandbox Code Playgroud)


Sha*_*ngh 6

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let home = storyBoard.instantiateViewController(withIdentifier: "HOMEVC") as! HOMEVC
navigationController?.pushViewController(home, animated: true);
Run Code Online (Sandbox Code Playgroud)