Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift中设置UITabBarController的视图控制器_Ios_Swift_Uitabbarcontroller - Fatal编程技术网

Ios 在Swift中设置UITabBarController的视图控制器

Ios 在Swift中设置UITabBarController的视图控制器,ios,swift,uitabbarcontroller,Ios,Swift,Uitabbarcontroller,我正在尝试以编程方式设置自定义TabBarController的视图控制器: import UIKit class TabBarViewController: UITabBarController, UITabBarControllerDelegate { var cameraViewController: UIViewController? var profileViewController: UIViewController? override func viewDidLoad() {

我正在尝试以编程方式设置自定义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]?
self.setViewControllers(controllers as! [AnyObject], animated: true)
我收到一个错误,无法将[UIViewController]转换为[AnyObject?]

和线

self.viewControllers = [cameraViewController, profileViewController] as! [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!]
    
  • 检查
    viewDidLoad
    中的
    cameraViewController
    profileViewController
    是否为零。但我觉得这个设计很糟糕

    if let c = cameraViewController, let p = profileViewController {
        self.viewControllers = [c, p]
    }
    

因此,它归结为如何初始化
cameraViewController
profileViewController
。它们是否在显示选项卡栏视图控制器之前设置?如果是这样,我建议在类中添加自定义的
init

cameraViewController和profileViewController实际上是脚本中的自定义UIViewController。所以我想第一个选项应该是最好的?你能解释一下为什么最后一个选项是糟糕的设计吗?只是我的观点:你应该确定这些变量在
viewDidLoad
中包含了什么。如果它们是
TabBarViewController
类所必需的,那么它们在那里应该是非零的,并且您应该100%确定这一点!现在,如果
TabBarViewController
是一个非常通用的类和函数,有没有这两个视图控制器,那么情况就不同了。但我从你的问题中假设它们是必要的:)你如何以及在哪里设置变量?听起来很完美!要明确从类中设置它们,可以将它们标记为
private(set)
。作为提示。