【问题标题】:Swift cannot assign value of type 'UINavigationController'Swift 无法分配“UINavigationController”类型的值
【发布时间】:2017-07-02 10:08:23
【问题描述】:

我是 Swift 的新手,所以我只是在学习。我正在关注 youtube 上的教程,但我不断收到此错误。让我们构建那个应用程序:如何构建 Twitter。'应用程序窗口应该在应用程序启动结束时有一个根视图控制器'

对于window?.rootViewController = UINavigationController 它说

无法将“UINavigationController”类型的值分配给类型 UIViewController?类型的表达式'(rootViewController: HomeController)' 未使用

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions:
    [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    // ignore storyboard.
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()

    let homeController = HomeContoller(collectionViewLayout:
        UICollectionViewFlowLayout())
    window?.rootViewController = UINavigationController
        (rootViewController: homeController)

    return true
}

func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


}

【问题讨论】:

  • 你有没有在 UINavigationController 和 ( ?
  • window?.rootViewController = UINavigationController (rootViewController: homeController) 将此行更改为 window?.rootViewController = homeController 它说你不能分配 UINavigationController 因为它需要一个 UIViewController。
  • @ManishPathak 但 UINavigationController 是 UIViewController。
  • @luk2302 你是对的,HomeController 的超类是什么?这是工作 self.window = UIWindow(frame: UIScreen.main.bounds) let storyboard = UIStoryboard(name: "Main", bundle: nil) let initialViewController = storyboard.instantiateViewController(withIdentifier: "identifierStringRootVC") let nav1 = UINavigationController( rootViewController: initialViewController) self.window?.rootViewController = nav1 self.window?.makeKeyAndVisible()
  • 还是不行

标签: ios swift


【解决方案1】:

删除这行之间的换行符:

window?.rootViewController = UINavigationController
    (rootViewController: homeController)

第一行:

window?.rootViewController = UINavigationController

产生这个错误:

无法将类型“UINavigationController.Type”的值分配给类型“UIViewController?”

第二行:

(rootViewController: homeController)

产生此警告:

'(rootViewController: HomeController)' 类型的表达式未使用

如果删除换行符,两者都会消失:

window?.rootViewController = UINavigationController(rootViewController: homeController)

【讨论】:

    【解决方案2】:

    试试这个:

    self.window = UIWindow(frame: UIScreen.main.bounds)
    
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    
    let initialViewController = storyboard.instantiateViewController(withIdentifier: "navigationController")
    
    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()
    

    注意:你需要把storyboard id“navigationController”放到你的navigationController中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多