gpt4 book ai didi

ios - 无法以编程方式在 Swift 中创建 UIViewController

转载 作者:搜寻专家 更新时间:2023-10-30 22:07:42 25 4
gpt4 key购买 nike

当我尝试在 Swift 中创建 UIViewController 的实例时,所有继承的初始化程序都不可用,即使我没有在 View 中定义任何指定的初始化程序 Controller (或其他任何东西,FWIW)。

此外,如果我尝试通过将其设为 Root View Controller 来显示它,它永远不会显示:

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
}

View Controller 的代码只是 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.
}
*/

}

有人知道这是怎么回事吗????

最佳答案

正如您所指出的:只要 nib 名称与 objective-C 中的类名称相匹配,即使您在初始化 View Controller 时未指定 nib 名称, View Controller 仍将 look for the nib file whose name matches the name of the view controller class .

但由于某些原因(也许是一个错误),在 Swift 中情况并非如此。

而不是写:

let vc = ImageViewController()

初始化 View Controller 时必须明确指定接口(interface):

let vc = ImageViewController(nibName: "nibName", bundle: nil)

关于ios - 无法以编程方式在 Swift 中创建 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27151026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com