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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30956476/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:31:37  来源:igfitidea点击:

Swift – Instantiating a navigation controller without storyboards in App Delegate

iosobjective-cswiftuinavigationcontrollerappdelegate

提问by Zack Shapiro

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.

我正在重建一个没有故事板的应用程序,其中我遇到的最麻烦的部分是以编程方式导航视图到视图。很少有不使用故事板的东西被写出来,所以找到这个问题的答案一直很困难。

My problem is pretty simple. I have my ViewControllerand my SecondViewControllerand I want to push from the former to the latter.

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

In AppDelegate:

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
}

Then in ViewController.swift:

然后在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)
    }
}

Printing self.navigationControllerreturns nil. I've tried doing:

打印self.navigationController返回零。我试过这样做:

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

var navController = UINavigationController()ViewController创建类(但在 ViewDidLoad 之外,就在类声明之下)并使用navControllervar完成推送时,但没有奏效。

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?

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

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

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

Thanks for taking a look and for your help.

感谢您的查看和帮助。

回答by jaiswal Rajan

In Swift 3

在斯威夫特 3

Place this code inside didFinishLaunchingWithOptions method in AppDelegate class.

将此代码放在 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()

回答by agy

In AppDelegate

在 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
}

In 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.
}


}