Swift – 以编程方式导航到另一个视图控制器/场景

在从第一个视图控制器导航到第二个视图控制器时,我收到了一条错误消息。 我的编码是这样的

let vc = LoginViewController(nibName: "LoginViewController", bundle: nil) self.navigationController?.pushViewController(vc, animated: true) 

问题是我总是得到这种错误信息

 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) 

请指教

我已经find了答案

 let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil) let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("nextView") as NextViewController self.presentViewController(nextViewController, animated:true, completion:nil) 

试试这个。 这里“LoginViewController”是故事板中指定的storyboardID。

见下文

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

XCODE 8.2和SWIFT 3.0

呈现一个存在的UIViewController

 let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController self.present(loginVC, animated: true, completion: nil) 

推一个存在的UIViewController

 let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController self.navigationController?.pushViewController(loginVC, animated: true) 

请记住,您可以按照以下步骤放置UIViewController标识符:

  • selectMain.storyboard
  • select你的UIViewController
  • search右侧的实用程序
  • select身份检查员
  • search部分标识“Storyboard ID”
  • 把你的UIViewController的标识符

在这里输入图像说明

看我的。

 func actioncall () { let loginPageView = self.storyboard?.instantiateViewControllerWithIdentifier("LoginPageID") as! ViewController self.navigationController?.pushViewController(loginPageView, animated: true) } 

如果您使用演示风格,则可能会丢失具有预设推送导航的页面导航栏。

您可以使用以下代码以编程方式从一个场景移动到另一个场景:

  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) 

这里的storyBoardID是你使用Interface Builder设置为场景的值。 如下所示:

如何设置storyboardID的屏幕截图

如果要导航到以编程方式创build的Controller,请执行以下操作:

 let newViewController = NewViewController() self.navigationController?.pushViewController(newViewController, animated: true) 

如果你想导航到带有标识符“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) 

Swift3:

 let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc = storyboard.instantiateViewController("LoginViewController) as UIViewController self.navigationController?.pushViewController(vc, animated: true) 

试试这个。 你只是与故事板表示困惑的笔尖。

我给你我的代码做一个转换。 在这个例子中,动作连接到一个UIButton。 所以不要忘了设置它。 不要忘记在transition方法中设置ViewController的名称。

不要忘记设置你的故事板了。 你需要每个viewController有一个视图。 将每个ViewController连接到storyBoard中的每个视图。 你可以在屏幕上看到下图

在这里输入图像说明在这里输入图像说明

 class PresentationViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() var playButton = UIButton.buttonWithType(UIButtonType.System) as UIButton let image = UIImage(named: "YourPlayButton") as UIImage? playButton.frame = CGRectMake(0, 0, 100, 100) playButton.center = CGPointMake(self.view.frame.width/2, self.view.frame.height/2) playButton.addTarget(self, action: "transition:", forControlEvents: UIControlEvents.TouchUpInside) playButton.setBackgroundImage(image, forState: UIControlState.Normal) self.view.addSubview(playButton) } func transition(sender:UIButton!) { println("transition") let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("YourSecondViewController") as UIViewController let window = UIApplication.sharedApplication().windows[0] as UIWindow UIView.transitionFromView( window.rootViewController!.view, toView: secondViewController.view, duration: 0.65, options: .TransitionCrossDissolve, completion: { finished in window.rootViewController = secondViewController }) } } 

您可以在视图控制器的storyboard和storyboardId的帮助下,使用代码在视图控制器之间进行导航。 这个代码是Swift 3:

 let signUpVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SignUp") self.navigationController?.pushViewController(signUpVC, animated: true) 
 let VC1 = self.storyboard!.instantiateViewController(withIdentifier: "MyCustomViewController") as! ViewController let navController = UINavigationController(rootViewController: VC1) self.present(navController, animated:true, completion: nil) 

我不确定下面的步骤尝试,我认为可能会出现以下错误的原因。

  • 您可以重命名XCode之外的某些文件。 要解决这个问题,请从项目中删除文件,然后重新导入项目中的文件。
  • Build阶段检查并添加缺less的Nib文件 – >复制包资源。最后检查笔尖名称的拼写,这是正确的,区分大小写。
  • 检查文件检查器中的.xib / storyboard文件的属性,在select框中select属性“ Target Membership ”,然后将xib / storyboard文件链接到目标文件。
  • 如不正确的types的NIB.Right点击该文件,然后单击“获取信息”,以validationtypes是你所期望的。

如果您正在构buildUI而不拖放(不使用storyboard)并且想要将默认页面或ViewController.swift导航到另一个页面? 遵循以下步骤1)添加一个类(.swift)2)导入UIKit 3)声明类名称

 class DemoNavigationClass :UIViewController{ override func viewDidLoad(){ let lbl_Hello = UILabel(frame: CGRect(x:self.view.frame.width/3, y:self.view.frame.height/2, 200, 30)); lbl_Hello.text = "You are on Next Page" lbl_Hello.textColor = UIColor.white self.view.addSubview(lbl_Hello) } } 

创build第二页后回到第一页(ViewController.swift)这里在viewDidLoad方法中做一个button

 let button = UIButton() button.frame = (frame: CGRect(x: self.view.frame.width/3, y: self.view.frame.height/1.5, width: 200, height: 50)) button.backgroundColor = UIColor.red button.setTitle("Go to Next ", for: .normal) button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside) self.view.addSubview(button) 

现在在同一个类的viewDidLoad()之外定义buttonAction方法

 func buttonAction(sender: UIButton!) { let obj : DemoNavigationClass = DemoNavigationClass(); self.navigationController?.pushViewController(obj, animated: true) } 

保留一件我在main.storyboard中忘记的东西,有一个箭头的场景,select那个箭头,然后按下删除button

现在拖放一个导航控制器,并删除与导航控制器一起提供的tableview。 select导航控制器按下键盘上的控制,并将其拖动到ViewController的故事板上的另一个场景。 这意味着你的viewcontroller成为根viewcontroller希望这可以帮助你在main.storyboard感谢,拖放navigationcontroller,

 let signUpVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SignUp") // self.present(signUpVC, animated: false, completion: nil) self.navigationController?.pushViewController(signUpVC, animated: true) 

除了上面的设置导航视图控制器在你的应用上的屏幕顶部的好答案,你可以添加到你的AppDelegate.swift文件在块内,如下

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { window = UIWindow() window?.makeKeyAndVisible() window?.rootViewController = UINavigationController(rootViewController: LoginViewController()) return true }