簡體   English   中英

在UITabBarController中聲明委托時,在兩個ViewController之間傳遞數據時出錯

[英]Error passing data between two ViewControllers when declaring the delegate within a UITabBarController

我試圖通過UITabBarController進行初始調用,從而在兩個ViewController之間傳遞數據。

這是我在做什么。 我正在使用一個名為RaisedTabBarController的類將自定義按鈕添加到TabBarController,可以很好地顯示該按鈕,我的問題是,當我點擊自定義按鈕時,我希望它將我帶到FirstViewController ,然后再從通過協議將SecondViewController FirstViewControllerSecondViewController ,但由於某種原因,我收到一個錯誤,我認為這沒有任何意義,它抱怨在SecondViewController無法訪問標簽。

這是錯誤:

致命錯誤:解開Optional值時意外發現nil

這是代碼...

來自GitHub的類參考:

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),問題已解決。

這似乎是錯誤的。

pvc.delegate = SecondViewController() as FirstViewControllerDelegate

嘗試像從情節提要中第一個一樣實例化SecondViewController。

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

然后將委托設置為SecondViewController

pvc.delegate = svc

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM