我尝试如下创建自定义UINavigationController
类-
class NavigationController: UINavigationController {
private let user: User
init(user: User, rootViewController: UIViewController) {
self.user = user
super.init(rootViewController: rootViewController)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
以上代码在运行时失败,并出现以下错误-
致命错误:对类“ app.NavigationController”使用未实现的初始化程序“ init(nibName:bundle :)”
我试图通过添加初始化程序来解决此问题
class NavigationController: UINavigationController {
private let user: User
init(user: User, rootViewController: UIViewController) {
self.user = user
super.init(rootViewController: rootViewController)
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
编译器将引发错误“未在super.init调用中初始化属性'self.user'”。
有没有一种方法可以解决此问题,而无需将user
属性设置为隐式展开的可选内容?
答案 0 :(得分:1)
您可以执行以下操作:
class NavigationController: UINavigationController {
private let user: User
init(user: User, rootViewController: UIViewController, nibName nibNameOrNil: String? = nil, bundle nibBundleOrNil: Bundle? = nil) {
self.user = user
super.init(nibName: nil, bundle: nil)
self.viewControllers = [rootViewController]
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
let vc = UIViewController()
let nc = NavigationController(user: User(), rootViewController: vc)
答案 1 :(得分:0)
不要像这样初始化用户。
在 Controller 中,您将其称为NavigationController
:
将private let user : User
更改为
var user : User!
初始化您的NavigationController
然后将用户设置为:
navigationController.user = self.user
self.user
是您要赋予navigationController
答案 2 :(得分:0)
方法
google.appengine.api.users.create_login_url
是指定的初始化程序,这意味着它将始终被调用,而
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?)
是一个便利初始化程序,出于便利原因可能会被调用。当指定的初始化程序被调用时,您的类具有未初始化的用户常数。您可以将user切换为可选的var,并使用nil对其进行初始化。
答案 3 :(得分:0)
使用带有自定义参数的便捷初始化并调用指定的初始化程序来设置root viewController
final class NavigationController: UINavigationController {
private var user: User?
convenience init(rootViewController: UIViewController, user: User) {
self.init(rootViewController: rootViewController)
self.user = user
}
override init(rootViewController: UIViewController) {
super.init(rootViewController: rootViewController)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
/// How to use
let user = User()
let rootViewController = UIViewController()
let navigationController = NavigationController(rootViewController: rootViewController, user: user)