在Swift中设置UITabBarController的视图控制器 [英] Set view controllers of UITabBarController in Swift

查看:94
本文介绍了在Swift中设置UITabBarController的视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式设置我的自定义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 in viewDidLoad

      self.viewControllers = [cameraViewController!,profileViewController!] 


  • 检查 cameraViewController profileViewController viewDidLoad 。这对我来说闻起来很糟糕。

     如果让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 and profileViewController are never nil in viewDidLoad:

    self.viewControllers = [cameraViewController!, profileViewController!]
    

  • Check if cameraViewController and profileViewController are not nil in viewDidLoad. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆