在 Swift 中设置 UITabBarController 的视图控制器
Set view controllers of UITabBarController in Swift
我正在尝试以编程方式设置自定义 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)
}
但是用线
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 和类型转换。
问题是您尝试使用的视图控制器被声明为可选的:
var cameraViewController: UIViewController?
var profileViewController: UIViewController?
所以你有三个选择:
不要让它们可选。这要求您在初始化 TabBarViewController
时用一些东西初始化它们。也许是最安全的选择。
如果你知道 cameraViewController
和 profileViewController
在 viewDidLoad
中永远不会是 nil
:
self.viewControllers = [cameraViewController!, profileViewController!]
检查 cameraViewController
和 profileViewController
是否在 viewDidLoad
中不为零。不过,这对我来说是糟糕的设计。
if let c = cameraViewController, let p = profileViewController {
self.viewControllers = [c, p]
}
因此归结为您如何初始化 cameraViewController
和 profileViewController
。它们是在显示标签栏视图控制器之前设置的吗?如果是这样,我建议将自定义 init
添加到您的 class.
我正在尝试以编程方式设置自定义 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)
}
但是用线
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 和类型转换。
问题是您尝试使用的视图控制器被声明为可选的:
var cameraViewController: UIViewController?
var profileViewController: UIViewController?
所以你有三个选择:
不要让它们可选。这要求您在初始化
TabBarViewController
时用一些东西初始化它们。也许是最安全的选择。如果你知道
cameraViewController
和profileViewController
在viewDidLoad
中永远不会是nil
:self.viewControllers = [cameraViewController!, profileViewController!]
检查
cameraViewController
和profileViewController
是否在viewDidLoad
中不为零。不过,这对我来说是糟糕的设计。if let c = cameraViewController, let p = profileViewController { self.viewControllers = [c, p] }
因此归结为您如何初始化 cameraViewController
和 profileViewController
。它们是在显示标签栏视图控制器之前设置的吗?如果是这样,我建议将自定义 init
添加到您的 class.