Swift - 在App Delegate中实例化没有故事板的导航控制器

时间:2015-06-20 16:43:15

标签: ios objective-c swift uinavigationcontroller appdelegate

我正在重建一个没有故事板的应用程序,而我遇到的最麻烦的部分是以编程方式导航视图。很少有东西写在那里没有使用故事板,所以找到答案是很困难的。

我的问题非常简单。我有ViewControllerSecondViewController,我想从前者推到后者。

AppDelegate

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    window?.backgroundColor = UIColor.whiteColor()
    window?.rootViewController = ViewController()
    window?.makeKeyAndVisible()

    return true
}

然后在ViewController.swift

class ViewController: UIViewController, AVAudioPlayerDelegate, UITextFieldDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        startFinishButton.setTitle("Begin", forState: .Normal)
        startFinishButton.addTarget(self, action: "moveToSecondViewController", forControlEvents: .TouchUpInside)
        view.addSubview <*> startFinishButton
    }

    func moveToSecondViewController(sender: UIButton) {
        let vc = SecondViewController()
        println(self.navigationController) // returns nil
        self.navigationController?.pushViewController(vc, animated: true)
    }
}

打印self.navigationController返回nil。我试过了:

var navController = UINavigationController()创建ViewController类时(但在ViewDidLoad之外,在类声明的正下方)并使用navController var完成推送,但是没有效果。

我想也许解决方案是在App Delegate中创建一个整个应用程序会使用的导航控制器,我想这是一个全局变量?

我希望这篇文章可以为许多其他新手提供服务,并希望从他们的应用中删除故事板。

感谢您了解并寻求帮助。

2 个答案:

答案 0 :(得分:36)

在Swift 3中

将此代码放在AppDelegate类的didFinishLaunchingWithOptions方法中。

window = UIWindow(frame: UIScreen.main.bounds)
let mainController = MainViewController() as UIViewController
let navigationController = UINavigationController(rootViewController: mainController)
navigationController.navigationBar.isTranslucent = false
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()

答案 1 :(得分:18)

在AppDelegate

var window: UIWindow?
var navController: UINavigationController?

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

    navController = UINavigationController()
    var viewController: ViewController = ViewController()
    self.navController!.pushViewController(viewController, animated: false)

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

    self.window!.rootViewController = navController

    self.window!.backgroundColor = UIColor.whiteColor()

    self.window!.makeKeyAndVisible()

    return true
}

在ViewController中

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    self.title = "FirstVC"

    var startFinishButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton
    startFinishButton.frame = CGRectMake(100, 100, 100, 50)
    startFinishButton.backgroundColor = UIColor.greenColor()
    startFinishButton.setTitle("Test Button", forState: UIControlState.Normal)
    startFinishButton.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

    self.view.addSubview(startFinishButton)
}

func buttonAction(sender:UIButton!)
{
    println("Button tapped")
    let vc = SecondViewController()
    self.navigationController?.pushViewController(vc, animated: true)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}