Swift - 在 App Delegate 中实例化没有故事板的导航控制器 [英] Swift – Instantiating a navigation controller without storyboards in App Delegate

查看:19
本文介绍了Swift - 在 App Delegate 中实例化没有故事板的导航控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重建一个没有故事板的应用程序,其中我遇到的最麻烦的部分是以编程方式导航视图到视图.很少有内容不使用故事板,因此很难找到答案.

I'm rebuilding an app without storyboards and the part of it that I'm having the most trouble with is navigating view-to-view programatically. Few things are written out there which don't use storyboards, so finding an answer for this has been tough.

我的问题很简单.我有我的 ViewController 和我的 SecondViewController,我想从前者推送到后者.

My problem is pretty simple. I have my ViewController and my SecondViewController and I want to push from the former to the latter.

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.我试过这样做:

Printing self.navigationController returns nil. I've tried doing:

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

var navController = UINavigationController() when the ViewController class is created (but outside of ViewDidLoad, right under the class declaration) and done the push using the navController var but that hasn't worked.

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

I'm thinking maybe the solution is to create a navigation controller in App Delegate that the whole app would use, I guess as a global variable?

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

My hope is that this post can serve many others who are new to Swift and want to remove storyboards from their app.

感谢您的浏览和帮助.

推荐答案

在 Swift 3 中

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

Place this code inside didFinishLaunchingWithOptions method in AppDelegate class.

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()

这篇关于Swift - 在 App Delegate 中实例化没有故事板的导航控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆