ios 如何使用 Swift 从一个视图控制器导航到另一个视图控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24038215/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:24:55  来源:igfitidea点击:

How to Navigate from one View Controller to another using Swift

iosswiftviewcontrolleruinavigationcontroller

提问by sathish

I'd like to navigate from one view controller to another. How can I convert the following Objective-C code into Swift?

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

UIViewController *viewController = [[self storyboard] instantiateViewControllerWithIdentifier:@"Identifier"];
UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.navigationController pushViewController:navi animated:YES];

回答by Audrey Sobgou Zebaze

Create a swift file (SecondViewController.swift) for the second view controller and in the appropriate function type this:

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

let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController
self.navigationController.pushViewController(secondViewController, animated: true)



Swift 2+

斯威夫特 2+

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


Swift 4

斯威夫特 4

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

回答by Mojtabye

In my experience navigationControllerwas nil so I changed my code to this:

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

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

Don't forget to set ViewController StoryBoard Idin StoryBoard-> identity inspector

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

回答by Keith Holliday

If you don't want the back button to appear (which was my case, because I wanted to present after a user logged in) here is how to set the root of the nav controller:

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

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

回答by Maksim Kniazev

SWIFT 3.01

斯威夫特 3.01

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

回答by Rahul Phate

In swift 4.0

在 Swift 4.0

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

回答by iOS

In Swift 4.1 and Xcode 10

在 Swift 4.1 和 Xcode 10 中

Here AddFileViewControlleris second view controller.

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

Storyboard id is AFVC

故事板 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)

If required use thread.

如果需要,请使用线程。

Ex:

前任:

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

If you want move after some time.

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

EX:

前任:

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

回答by SAURAV JUSTIN

Swift 3

斯威夫特 3

let secondviewController:UIViewController =  self.storyboard?.instantiateViewController(withIdentifier: "StoryboardIdOfsecondviewController") as? SecondViewController

self.navigationController?.pushViewController(secondviewController, animated: true)

回答by Davender Verma

let objViewController = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
self.navigationController?.pushViewController(objViewController, animated: true)

回答by Zeeshan

In swift 3

在迅捷 3

let nextVC = self.storyboard?.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController        
self.navigationController?.pushViewController(nextVC, animated: true)

回答by Azharhussain Shaikh

Swift 4

斯威夫特 4

You can switch the screen by pushing navigation controller first of all you have to set the navigation controllerwith UIViewController

您可以通过按下导航控制器来切换屏幕首先您必须使用UIViewController设置导航控制器

let vc = self.storyboard?.instantiateViewController(withIdentifier: "YourStoryboardID") as! swiftClassName

self.navigationController?.pushViewController(vc, animated: true)