ios 以编程方式导航到另一个视图控制器/场景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27374759/
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
Programmatically navigate to another view controller/scene
提问by Mohammad Nurdin
I got an error message during navigating from first view controller to second view controller. My coding is like this one
我在从第一个视图控制器导航到第二个视图控制器时收到一条错误消息。我的编码是这样的
let vc = LoginViewController(nibName: "LoginViewController", bundle: nil)
self.navigationController?.pushViewController(vc, animated: true)
The problem is I always got this kind of error message
问题是我总是收到这种错误信息
2014-12-09 16:51:08.219 XXXXX[1351:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </var/mobile/Applications/FDC7AA0A-4F61-47E7-955B-EE559ECC06A2/XXXXX.app> (loaded)' with name 'LoginViewController''
*** First throw call stack:
(0x2efcaf0b 0x39761ce7 0x2efcae4d 0x31b693f9 0x31ac1eaf 0x3191e365 0x317fe895 0x318a930d 0x318a9223 0x318a8801 0x318a8529 0x318a8299 0x318a8231 0x317fa305 0x3147631b 0x31471b3f 0x314719d1 0x314713e5 0x314711f7 0x3146af1d 0x2ef96039 0x2ef939c7 0x2ef93d13 0x2eefe769 0x2eefe54b 0x33e6b6d3 0x3185d891 0x4ccc8 0x4cd04 0x39c5fab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
回答by Mohammad Nurdin
I already found the answer
我已经找到答案了
Swift 4
斯威夫特 4
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "nextView") as! NextViewController
self.present(nextViewController, animated:true, completion:nil)
Swift 3
斯威夫特 3
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("nextView") as NextViewController
self.presentViewController(nextViewController, animated:true, completion:nil)
回答by user2885077
Try this one. Here "LoginViewController" is the storyboardID specified in storyboard.
试试这个。这里“LoginViewController”是storyboard中指定的storyboardID。
See below
见下文
let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as LoginViewController
self.navigationController?.pushViewController(secondViewController, animated: true)
回答by Cristian Mora
XCODE 8.2 AND SWIFT 3.0
Xcode 8.2 和 Swift 3.0
Present an exist UIViewController
存在 UIViewController
let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
self.present(loginVC, animated: true, completion: nil)
Push an exist UIViewController
推送一个存在 UIViewController
let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
self.navigationController?.pushViewController(loginVC, animated: true)
Remember that you can put the UIViewController
Identifier following the next steps:
请记住,您可以按照UIViewController
以下步骤放置标识符:
- Select
Main.storyboard
- Select your
UIViewController
- Search the Utilities in the right
- Select the identity inspector
- Search in section identity "Storyboard ID"
- Put the Identifier for your
UIViewController
- 选择
Main.storyboard
- 选择您的
UIViewController
- 搜索右侧的实用程序
- 选择身份检查员
- 在部分身份“故事板 ID”中搜索
- 把标识符放在你的
UIViewController
回答by jaiswal Rajan
If you want to navigate to Controller created Programmatically, then do this:
如果要导航到以编程方式创建的控制器,请执行以下操作:
let newViewController = NewViewController()
self.navigationController?.pushViewController(newViewController, animated: true)
If you want to navigate to Controller on StoryBoard with Identifier "newViewController", then do this:
如果要使用标识符“newViewController”导航到 StoryBoard 上的 Controller,请执行以下操作:
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "newViewController") as! NewViewController
self.present(newViewController, animated: true, completion: nil)
回答by A.G
See mine.
看我的。
func actioncall () {
let loginPageView = self.storyboard?.instantiateViewControllerWithIdentifier("LoginPageID") as! ViewController
self.navigationController?.pushViewController(loginPageView, animated: true)
}
If you use presenting style, you might lose the page's navigation bar with preset pushnavigation.
如果您使用呈现样式,您可能会丢失带有预设推送导航的页面导航栏。
回答by Jayprakash Dubey
You can move from one scene to another programmatically using below code :
您可以使用以下代码以编程方式从一个场景移动到另一个场景:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let objSomeViewController = storyBoard.instantiateViewControllerWithIdentifier(“storyboardID”) as! SomeViewController
// If you want to push to new ViewController then use this
self.navigationController?.pushViewController(objSomeViewController, animated: true)
// ---- OR ----
// If you want to present the new ViewController then use this
self.presentViewController(objSomeViewController, animated:true, completion:nil)
Here storyBoardID is value that you set to scene using Interface Builder. This is shown below :
这里的 storyBoardID 是您使用 Interface Builder 为场景设置的值。这如下所示:
回答by Arif Fikri Abas
Swift3:
斯威夫特3:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController("LoginViewController") as UIViewController
self.navigationController?.pushViewController(vc, animated: true)
Try this out. You just confused nib with storyboard representation.
试试这个。您只是将笔尖与故事板表示混淆了。
回答by Bhanu
Programmatically there are different ways based on different situations
编程上根据不同情况有不同的方法
load storyenter code hereboard nib file
let yourVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "YourViewController") as! YourViewController; self.present(yourVc, animated: true, completion: nil)
Load from xib
let yourVc = YourViewController.init(nibName: "YourViewController", bundle: nil) self.present(yourVc, animated: true, completion: nil)
Navigate through Segue
self.performSegue(withIdentifier: "your UIView", sender: self)
在此处加载 storyenter 代码 nib 文件
let yourVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "YourViewController") as! YourViewController; self.present(yourVc, animated: true, completion: nil)
从xib加载
let yourVc = YourViewController.init(nibName: "YourViewController", bundle: nil) self.present(yourVc, animated: true, completion: nil)
浏览 Segue
self.performSegue(withIdentifier: "your UIView", sender: self)
回答by MrMins
let VC1 = self.storyboard!.instantiateViewController(withIdentifier: "MyCustomViewController") as! ViewController
let navController = UINavigationController(rootViewController: VC1)
self.present(navController, animated:true, completion: nil)
回答by Krishna Vivekananda
You can do navigation between view controllers using code with the help of storyboard and storyboardId of the view controller. This code is for Swift 3:
您可以在视图控制器的 storyboard 和 storyboardId 的帮助下使用代码在视图控制器之间进行导航。此代码适用于 Swift 3:
let signUpVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SignUp")
self.navigationController?.pushViewController(signUpVC, animated: true)