我是斯威夫特的新手,所以我只是在学习。我正在关注youtube上的一个教程,我一直收到这个错误。让我们构建该应用程序:如何构建Twitter。应用程序启动结束时,应用程序窗口应该有一个根视图控制器“
对于它所说的window?.rootViewController = UINavigationController
无法将类型'UINavigationController'的值指定为type 的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:.
}
}
答案 0 :(得分:4)
删除这些两个行之间的换行符:
window?.rootViewController = UINavigationController
(rootViewController: homeController)
第一行:
window?.rootViewController = UINavigationController
产生此错误:
无法指定'UINavigationController.Type'类型的值来输入'UIViewController?'
和第二行:
(rootViewController: homeController)
产生此警告:
类型'(rootViewController:HomeController)'的表达式未使用
如果你删除换行符,两者都会消失:
window?.rootViewController = UINavigationController(rootViewController: homeController)
答案 1 :(得分:-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