0
我想开始一个新的Swift项目。这是我第一次尝试以编程方式创建视图。但它甚至不像我的控制器正在加载?我看到的只是启动屏幕,然后在加载到模拟器上时出现黑屏。Swift:设置rootViewController不工作?
这是我的AppDelegate:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
NSLog("zrrrzz") // <------------------------------ Prints properly
self.window?.rootViewController = self.rootViewController()
return true
}
private func rootViewController() -> UIViewController {
NSLog("zzz") // <---------------------------------- Does not print ????
return MapViewController.init()
}
}
的MapViewController:
import UIKit
class MapViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var label = UILabel(frame: CGRectMake(0, 0, 200, 21))
label.center = CGPointMake(160, 284)
label.textAlignment = NSTextAlignment.Center
label.text = "I am a test label"
self.view.backgroundColor = UIColor.whiteColor()
self.view.addSubview(label)
NSLog("heyyyy!!") //<------------------------------ Also doesn't print ??
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
我失踪了一步?我没有看到任何警告/错误,当我启动模拟器
在不工作Swift 3.0 –
@MayankJain它为什么会这样,它是从一年前开始的。 –
是的,你是对的...我在Swift 3面临同样的问题在这里看到我的问题https://stackoverflow.com/questions/40085021/initial-rootviewcontroller-is-not-setting-in-swift-3-0 –