我正试图以编程方式设置自定义TabBarController的视图控制器:
import UIKit
class TabBarViewController: UITabBarController, UITabBarControllerDelegate {
var cameraViewController: UIViewController?
var profileViewController: UIViewController?
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
//self.viewControllers = [cameraViewController, profileViewController] as! [AnyObject]?
let controllers: [UIViewController?] = [cameraViewController, profileViewController]
self.setViewControllers(controllers as! [AnyObject], animated: true)
}
但是使用 line 进行
self.viewControllers = [cameraViewController, profileViewController] as! [AnyObject]?
我遇到了一个错误,无法将[UIViewController]转换为[AnyObject?]
以下是相关代码:
self.setViewControllers(controllers as! [AnyObject], animated: true)
我收到一个错误,提示说:
Cannot invoke 'setViewControllers' with an argument list of type '([AnyObject], animated: Bool)'
我的问题与 AnyObject 和类型转换有关。
viewDidLoad
中这些变量包含了什么。如果它们对于TabBarViewController
类至关重要,那么它们应该在那里被设置为非空,并且您应该百分之百确定这一点!现在,如果TabBarViewController
是一个非常通用的类,并且可以在有或没有这两个视图控制器的情况下工作,那么情况就不同了。但是根据您的问题,我假设它们是必需的 :) - fabian789cameraViewController
和profileViewController
都在required init (coder aDecoder: NSCoder) {}
函数中设置。我使用storyboard.instantiateViewControllerWithIdentifier()
来实例化这些视图控制器。 - Superian007private(set)
。这只是一个提示。 - fabian789