如何在swift中将一页导航到另一页?

如何在swift中将一页导航到另一页?,swift,uiviewcontroller,uinavigationcontroller,Swift,Uiviewcontroller,Uinavigationcontroller,我正在用Swift处理从一个视图传递到另一个视图的数据。我正在使用以下代码: @IBAction func loginAction(sender: AnyObject) { let nextViewController = NextViewController(nibName: "NextViewController", bundle: nil) self.navigationController?.pushViewController(nextViewController, animated

我正在用Swift处理从一个视图传递到另一个视图的数据。我正在使用以下代码:

@IBAction func loginAction(sender: AnyObject) {
let nextViewController = NextViewController(nibName: "NextViewController", bundle: nil) 
self.navigationController?.pushViewController(nextViewController, animated: true) 
}

但是,它不会浏览第二页,为什么?

如果您不使用故事板,可以使用以下代码: AppDelegate.swift

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var userInputViewContrller: UserInputViewController = UserInputViewController()
    var navigationViewController: UINavigationController = UINavigationController()


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        self.window = UIWindow.init(frame: UIScreen.mainScreen().bounds)
        self.window?.backgroundColor = UIColor.whiteColor()
        self.navigationViewController = UINavigationController.init(rootViewController: userInputViewContrller)
        self.window?.rootViewController = self.navigationViewController
        self.window?.makeKeyAndVisible()

        return true
    }
现在,您已经将窗口的rootViewController设置为NavigationViewController,该控制器使用“userInputViewContrller”页面的变量进行初始化

单击UserInputViewController中的“单击此处”按钮,您将导航到下一屏幕项ViewController

import UIKit

class ItemViewController: UIViewController {
    override func viewWillAppear(animated: Bool) {
        //View

        let viewFrame = CGRect(x: 1, y: 30, width: 373, height: 635)
        let view: UIView = UIView(frame: viewFrame)
        self.view.addSubview(view)

        //Header Label
        let headerLabelFrame = CGRect(x: 25, y: 50, width: 325, height: 30)
        var headerLabel:UILabel = UILabel()
        headerLabel = UILabel(frame: headerLabelFrame)
        headerLabel.text = "Hello I'm in itemViewVireControllr"
        view.addSubview(headerLabel)
  }
}

您可以检查这种类型的另一个问题:的可能重复,但我没有使用故事板,我只使用XIB__Laura CalinoUplate添加更多代码。
self.navigationController
是否非空?
import UIKit

class ItemViewController: UIViewController {
    override func viewWillAppear(animated: Bool) {
        //View

        let viewFrame = CGRect(x: 1, y: 30, width: 373, height: 635)
        let view: UIView = UIView(frame: viewFrame)
        self.view.addSubview(view)

        //Header Label
        let headerLabelFrame = CGRect(x: 25, y: 50, width: 325, height: 30)
        var headerLabel:UILabel = UILabel()
        headerLabel = UILabel(frame: headerLabelFrame)
        headerLabel.text = "Hello I'm in itemViewVireControllr"
        view.addSubview(headerLabel)
  }
}