cfi*_*her 6 cocoa-touch initializer uiviewcontroller ios swift
当我尝试创建一个UIViewControllerin 的实例时Swift,即使我没有在视图控制器中定义任何指定的inits(或其他任何东西,FWIW),所有继承的初始化器都不可用.
此外,如果我尝试通过使其成为根视图控制器来显示它,它永远不会显示:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.makeKeyAndVisible()
window?.backgroundColor = UIColor.greenColor()
let vc = ImageViewController()
window?.rootViewController = vc
return true
}
Run Code Online (Sandbox Code Playgroud)
视图控制器的代码只是Xcode的模板:
import UIKit
class ImageViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
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.
}
*/
}
Run Code Online (Sandbox Code Playgroud)
谁知道发生了什么?
正如您所指出的:只要nib名称与objective-C中的类名匹配,即使在初始化视图控制器时未指定nib名称,视图控制器仍将查找名称为nib的文件匹配视图控制器类的名称.
但由于某种原因(也许这是一个错误),Swift的情况并非如此.
而不是写:
let vc = ImageViewController()
Run Code Online (Sandbox Code Playgroud)
初始化视图控制器时必须显式指定接口:
let vc = ImageViewController(nibName: "nibName", bundle: nil)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10712 次 |
| 最近记录: |