在UITabBarController中声明委托时,在两个ViewController之间传递数据时出错

时间:2017-06-17 14:59:09

标签: ios swift uitabbarcontroller

我正在尝试在两个ViewControllers之间传递数据,初始调用来自UITabBarController

这就是我正在做的事情。我正在使用一个名为RaisedTabBarController的类来向TabBarController添加一个自定义按钮,它可以很好地显示按钮,我的问题是当我点击自定义按钮时我希望它带我去FirstViewController然后我想通过协议将数据从FirstViewController传递到SecondViewController但由于某种原因我得到的错误在我看来没有任何意义,它抱怨标签无法访问在SecondViewController内。

这是错误:

  
    

致命错误:在解包可选值时意外发现nil

  

这是代码......

来自GitHub的Class ref:

RaisedTabBarController

TabBarController

我在这里添加自定义按钮并调用转到FirstViewController

import UIKit
/// TabBarController subclasses RaisedTabBarController
class TabBarController: RaisedTabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Insert empty tab item at center index. In this case we have 5 tabs.
        self.insertEmptyTabItem("", atIndex: 2)
        // Raise the center button with image
        let img = UIImage(named: “myImage”)
        self.addRaisedButton(img, highlightImage: nil, offset: -10.0)
    }
    // Handler for raised button
    override func onRaisedButton(_ sender: UIButton!) {
        super.onRaisedButton(sender)
        // Go to FirstViewController
        let pvc = storyboard?.instantiateViewController(withIdentifier: “firstStoryBoardID”) as! FirstViewController
        /// Here, I’m not sure if this is the right way to tell that 
        /// SecondViewController will be the delegate not TabBarController, seem to work
        pvc.delegate = SecondViewController() as FirstViewControllerDelegate
        self.present(pvc, animated:true, completion:nil)
    }
}

FirstViewController

从这里开始我想将数据发送到SecondViewController

protocol FirstViewControllerDelegate {
    func messageData(greeting: String)
}

class FirstViewController: UIViewController{
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    func sendData() {
        self.delegate?.messageData(greeting: “Hello SecondViewController”)
    }
}

SecondViewController

这里我想接收从FirstViewController发送的数据

class SecondViewController: UIViewController, FirstViewControllerDelegate{

    @IBOutlet weak var labelMessage: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    func messageData(greeting: String) {
        /// I do get message from FirstViewController
        print(" Message received from FirstViewController: \(greeting)")

        /// Here I get error, fatal error: unexpectedly found nil while unwrapping an Optional value
        ///  I think it has something to do with the labelMessage not being accessible, but why? 
        labelMessage.text = greeting
    }
}

我知道为什么我在SecondViewController中收到错误,如果在SecondViewController中声明它们,为什么不能访问标签?

理想情况下,我希望能够直接从SecondViewController调用方法onRaisedButton(_ sender: UIButton!),但无需继承RaisedTabBarController。如果这可以解决错误,我不是usr,但我认为这会使我的代码更清晰。

编辑:06/19/2017 - 解决了

我正在寻找的效果可以直接在故事板中的XCode中完成。我停止使用第三方类(RaisedTabBarController),问题解决了。

1 个答案:

答案 0 :(得分:1)

这似乎不对。

pvc.delegate = SecondViewController() as FirstViewControllerDelegate

尝试像第一个故事板那样实例化SecondViewController。

let svc = storyboard?.instantiateViewController(withIdentifier: “secondStoryBoardID”) as! SecondViewController

然后将委托设置为SecondViewController

pvc.delegate = svc