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

Swift – Instantiating a navigation controller without storyboards in App Delegate

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

我的问题很简单。我有 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 returns 无。我尝试过:

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

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

我希望这个 post 可以为 Swift 的许多其他人提供服务,他们希望从他们的应用程序中删除故事板。

感谢您的浏览和帮助。

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


}

在Swift3

将此代码放在 AppDelegate 的 didFinishLaunchingWithOptions 方法中 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 5 和 Xcode 13 中有一个 SceneDelegate 和 AppDelegate。所以现在要从项目中完全删除故事板并将视图控制器嵌入导航控制器中,请执行以下操作:

  1. 删除项目导航器中的实际故事板

  2. Select Project Navigator 中的项目,select Target,然后是 General 选项卡,然后从 Main Interface 中删除 Storyboard

  3. 在 info.plist 文件中删除故事板:

    应用程序场景清单 > 场景配置 > 应用程序会话角色 > 项目 0(默认配置)> 故事板名称

然后在场景委托中将场景函数更改为如下所示:

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    guard let windowScene = (scene as? UIWindowScene) else { return }
    let window = UIWindow(windowScene: windowScene)
    let navigationController = UINavigationController(rootViewController: YourViewController())
    window.rootViewController = navigationController
    self.window = window
    window.makeKeyAndVisible()
}