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)
let mapViewControllerObj = self.storyboard?.instantiateViewControllerWithIdentifier("MapViewControllerIdentifier") as? MapViewController
self.navigationController?.pushViewController(mapViewControllerObj!, animated: true)
Run Code Online (Sandbox Code Playgroud)
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)
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 Id
在StoryBoard
- >中设置ViewControlleridentity inspector
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)
在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)
在 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)
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)
归档时间: |
|
查看次数: |
198947 次 |
最近记录: |