【问题标题】:TableViews not working with TabBarController SwiftTableViews 不能与 TabBarController Swift 一起使用
【发布时间】:2019-05-25 12:22:21
【问题描述】:

我有一个带有tableView 的应用程序。我决定用其他tableViews 添加更多屏幕,所以我以编程方式添加了tabBarController。现在我在这些行上得到一个 found nil 错误

    tableView.delegate = self
    tableView.dataSource = self

如果我删除它们,tableView 不会加载。你知道我可能做错了什么吗?

我在主情节提要上添加了一个tabBarController 并将其链接到 swift 文件,但它也不起作用。

        class SecondVC: UIViewController,UITableViewDelegate, UITableViewDataSource {

        @IBOutlet weak var tableView: UITableView!

        var SoundsClasss = [SoundsClass]()

        override func viewDidLoad() {
        super.viewDidLoad()

         let p1 = SoundsClass(imageURL: "sound 01", audioURL: "01", videoTitle: "1", duration: 100)
         let p2 = SoundsClass(imageURL: "sound 01", audioURL: "02", videoTitle: "2", duration: 100)

         SoundsClasss.append(p1)
         SoundsClasss.append(p2)

         tableView.delegate = self
         tableView.dataSource = self

         }

TabBarController 的代码。我是否必须在此处更改任何内容以指定视图为tableView

        class MainTabBarController: UITabBarController {

             override func viewDidLoad() {
              super.viewDidLoad()

                 tabBar.barTintColor = UIColor.white
                 setupTabBar()

                }

             func setupTabBar(){

              let FirstController = UINavigationController(rootViewController: MainVC())
              let SecondController = UINavigationController(rootViewController: SecondVC()) 
              let ThirdController = UINavigationController(rootViewController: ThirdVC())

              viewControllers = [FirstController, SecondController,ThirdController]

              guard let items = tabBar.items else { return }        
              for item in items {
                    item.imageInsets = UIEdgeInsets(top: 4, left: 0, bottom: -4, right: 0)
                   }
                }

             }

【问题讨论】:

  • 什么是 nil,tableView 变量?
  • 这可能是表格视图的无效出口,但请上传您的代码并与我们分享,以便轻松检测问题
  • 要添加标签栏控制器,您应该在情节提要中选择您的SecondVC 并选择Editor->Embed In->Tab Bar Controller
  • 如何创建SecondVC的实例?
  • 是的,tableview 是 nil。表视图 UITableView?零。这是我得到的错误:“致命错误:在隐式展开可选值时意外发现 nil 2019-05-25 21:41:27.889311+0900 Sounds[16006:583763] 致命错误:在隐式展开可选值时意外发现 nil (lldb) "

标签: ios swift tableview uitabbarcontroller tabbarcontroller


【解决方案1】:

在名为 SecondVC 的类中,您有 UITableView 实例的出口引用,这意味着您在 Stroyboard 中创建表视图,因此您不能使用应该使用的初始化程序创建视图控制器

UIStoryboard(name: "yourStoryBoardName", bundle: nil).instantiateViewController(withIdentifier: "yourViewcontrollerID")

setupTabBar 函数应该如下

 func setupTabBar(){

    let vc1 = UIStoryboard(name: "yourStoryBoardName", bundle: nil).instantiateViewController(withIdentifier: "yourFirstViewcontrollerID")
    let vc2 = UIStoryboard(name: "yourStoryBoardName", bundle: nil).instantiateViewController(withIdentifier: "yourSecondViewcontrollerID")
    let vc3 = UIStoryboard(name: "yourStoryBoardName", bundle: nil).instantiateViewController(withIdentifier: "yourThirdViewcontrollerID")

    let FirstController = UINavigationController(rootViewController: vc1)
    let SecondController = UINavigationController(rootViewController: vc2)
    let ThirdController = UINavigationController(rootViewController: vc3)

    viewControllers = [FirstController, SecondController,ThirdController]

    guard let items = tabBar.items else { return }
    for item in items {
        item.imageInsets = UIEdgeInsets(top: 4, left: 0, bottom: -4, right: 0)
    }
}

【讨论】:

  • 谢谢!!这是对的。我不知道如何初始化它,这解决了问题
【解决方案2】:

您正在使用SecondVC() 创建您的控制器。这不引用情节提要,因此它不设置任何出口。

您需要在情节提要中构建视图控制器层次结构并让它默认加载,或者在将它们添加到导航控制器之前从情节提要中获取控制器。

请参阅以下文档:

func instantiateViewController(withIdentifier identifier: String) -> UIViewController

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 2012-08-03
    相关资源
    最近更新 更多