在Swift中设置UITabBarController的视图控制器 [英] Set view controllers of UITabBarController in Swift
问题描述
我正在尝试以编程方式设置我的自定义TabBarController的视图控制器:
import UIKit
class TabBarViewController:UITabBarController,UITabBarControllerDelegate {
var cameraViewController:UIViewController?
var profileViewController:UIViewController?
覆盖func viewDidLoad(){
super.viewDidLoad()
self.delegate = self
// self.viewControllers = [cameraViewController,profileViewController] as! [AnyObject]?
让控制器:[UIViewController?] = [cameraViewController,profileViewController]
self.setViewControllers(控制器为![AnyObject],动画:true)
}
但是行
self.viewControllers = [cameraViewController,profileViewController] as! [AnyObject]?
我收到错误,我无法将[UIViewController]转换为[AnyObject?]
和行
self.setViewControllers(控制器为![AnyObject],动画: true)
我收到错误消息:
无法使用类型'([AnyObject],animated:Bool)'
我的问题是AnyObject和类型转换我想。
问题是您尝试使用的视图控制器被声明为可选:
var cameraViewController:UIViewController?
var profileViewController:UIViewController?
所以你有三个选择:
-
不要让它们成为可选项。这要求您在初始化
TabBarViewController
时初始化它们。也许是最安全的选择。 -
如果您知道
cameraViewController
和profileViewController
永远nil
inviewDidLoad
:self.viewControllers = [cameraViewController!,profileViewController!]
-
检查
cameraViewController
和profileViewController
在viewDidLoad $中是否为零C $ C>。这对我来说闻起来很糟糕。
如果让c = cameraViewController,让p = profileViewController {
self。 viewControllers = [c,p]
}
因此,归结为如何初始化 cameraViewController
和 profileViewController
。是否在显示标签栏视图控制器之前设置了它们?如果是这样,我建议您在班级中添加自定义 init
。
I am trying to programatically set view controllers of my custom 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)
}
But with line
self.viewControllers = [cameraViewController, profileViewController] as! [AnyObject]?
I get an error that I cannot convert [UIViewController] to [AnyObject?]
and with line
self.setViewControllers(controllers as! [AnyObject], animated: true)
I get an error saying:
Cannot invoke 'setViewControllers' with an argument list of type '([AnyObject], animated: Bool)'
My problem is with AnyObject and typecasting I guess.
The problem is that the view controllers you are trying to use are declared optional:
var cameraViewController: UIViewController?
var profileViewController: UIViewController?
So you have three options:
Don't make them optional. This requires that you initialize them with something when you initalize your
TabBarViewController
. Maybe the safest option.If you know that
cameraViewController
andprofileViewController
are nevernil
inviewDidLoad
:self.viewControllers = [cameraViewController!, profileViewController!]
Check if
cameraViewController
andprofileViewController
are not nil inviewDidLoad
. This smells like bad design to me though.if let c = cameraViewController, let p = profileViewController { self.viewControllers = [c, p] }
So it boils down to how you initialize cameraViewController
and profileViewController
. Are they set before the tab bar view controllers is shown? If so, I recommend adding a custom init
to your class.
这篇关于在Swift中设置UITabBarController的视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!