【问题标题】:Changing RootViewController type in Swift在 Swift 中更改 RootViewController 类型
【发布时间】:2016-10-17 14:46:55
【问题描述】:

好的,我一直在学习一个教程,我已经完成了,一切正常。然而,加载的初始视图是 UITableViewController,我想要一个 UIViewController。

下面是代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()
    
    window?.rootViewController = UINavigationController(rootViewController: ViewController())
    
    return true
}

我已尝试编辑此行:

UINavigationController(rootViewController: ViewController())

到:

window?.rootViewController = UIViewController(rootViewController: ViewController())

然后我得到了这个错误:

调用中的参数标签不正确(有 'rootViewController:',预期为 'coder:')

然后它要求我“修复”,所以我这样做了,这会将行更改为:

window?.rootViewController = UIViewController(coder: ViewController())

但是现在这会引发错误:

无法将“ViewController”类型的值转换为预期的参数类型“NSCoder”

我也试过了:

window?.rootViewController = ViewController()

但这样一来,模拟器就会变黑。

澄清问题:

如何让我的应用中加载的第一个视图为 UIViewController 类型?

【问题讨论】:

    标签: ios swift rootviewcontroller


    【解决方案1】:

    你应该继承 UIViewController 并制作你自己的版本来检查它是否有效,但你最初所做的很好

    let myViewController = SomeViewController()
    let navigationController = UINavigationController(rootViewController: myViewController)
    window?.rootViewController = navigationController
    

    然后在SomeViewControllerviewDidLoad设置view.backgroundColor = .red

    如果要移除导航栏,可以将其设置为隐藏

    navigationController.navigationBarHidden = true
    

    或者...

    let myViewController = SomeViewController()
    window?.rootViewController = myViewController
    

    也可以使用...虽然您应该希望保留导航控制器的一般性...它通常会使将来呈现视图控制器更容易...

    您的模拟器变黑的原因是因为它可以工作...您显示的是一个空的UIViewController...您必须创建自己的UIViewController 子类并向其添加内容。

    您的 View Controller 子类应如下所示

    //
    //  SomeViewController.swift
    //  SomeProject
    //
    //  Created by Magoo on 17/10/2016.
    //  Copyright © 2016 Magoo. All rights reserved.
    //
    
    import UIKit
    
    class SomeViewController: UIViewController {
    
        override func viewDidLoad() {
    
            super.viewDidLoad()
            view.backgroundColor = .red
    
            let label = UILabel(frame:view.bounds)
            label.textColor = UIColor.whiteColor()
            label.text = "Hello world"
    
            view.addSubview(label)
        }
    }
    

    结果应该是一个红色的屏幕,中间写着“Hello world”。

    【讨论】:

    • 抱歉,有点新。你是什​​么意思:'你必须创建你自己的 UIViewController 子类并向它添加东西。你能解释一下吗?我已经创建了一个类型为 UIViewController 的新文件,其中包含 'class SomeViewController: UIViewController {'
    • 在 XCode 中,转到 File -> New -> File 并选择 Swift File。然后将其命名为 SomeViewController.swift ...然后该文件将在您的项目中可供您使用...您可以根据需要对其进行编辑。
    • 在这种情况下,您想在屏幕上显示的所有内容基本上都需要在SomeViewController 中,这就是显示的视图。
    • 感谢您精彩而详细的解释。非常感谢。
    • 不客气。我也添加了一个标签以更好地解释我的观点
    猜你喜欢
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多