【问题标题】:Swift – Instantiating a navigation controller without storyboards in App DelegateSwift – 在 App Delegate 中实例化没有故事板的导航控制器
【发布时间】:2015-09-06 12:27:15
【问题描述】:

我正在重建一个没有情节提要的应用程序,其中我遇到最大问题的部分是以编程方式导航视图到视图。很少有不使用情节提要的东西写出来,因此很难找到答案。

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

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 返回零。我试过这样做:

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

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

我希望这篇文章可以为许多刚接触 Swift 并希望从他们的应用程序中删除故事板的人提供帮助。

感谢您的浏览和帮助。

【问题讨论】:

    标签: ios objective-c swift uinavigationcontroller appdelegate


    【解决方案1】:

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

    【讨论】:

    • 你有什么自定义的navigationController?
    • 在 Xcode 11 和 Swift 5 中,您需要在 SceneDelegate.swift 中而不是在 AppDelegate.swift 中进行此操作 - 情况会发生一些变化。
    【解决方案2】:

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

    【讨论】:

    • 为什么要将 viewcontroller 设置为 uiviewcontroller 而不是我的第一个视图控制器 ViewController.swift?
    • @ZackShapiro,我编辑了我的答案。我用这段代码在 Xcode 中创建了一个示例,它可以工作。
    • 最后一个问题,如何在我的各种视图控制器中使用navController 来推送/弹出/等?
    • @ZackShapiro,我编辑了我的答案来解释如何使用 navController。如果您认为正确,请接受我的回答。
    猜你喜欢
    • 2013-01-10
    • 2013-02-03
    • 2021-08-05
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多