Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.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

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 在UITabBarController中声明代理时,在两个ViewController之间传递数据时出错_Ios_Swift_Uitabbarcontroller - Fatal编程技术网

Ios 在UITabBarController中声明代理时,在两个ViewController之间传递数据时出错

Ios 在UITabBarController中声明代理时,在两个ViewController之间传递数据时出错,ios,swift,uitabbarcontroller,Ios,Swift,Uitabbarcontroller,我尝试在两个ViewController之间传递数据,初始调用由UITabBarController进行 这就是我正在做的。我正在使用一个名为RaisedTabBarController的类向TabBarController添加一个自定义按钮,它可以很好地显示按钮,我的问题是,当我点击“自定义”按钮时,我希望它将我带到FirstViewController,然后我希望通过协议将数据从FirstViewController传递到SecondViewController,但由于某种原因,我得到了一个

我尝试在两个ViewController之间传递数据,初始调用由UITabBarController进行

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

以下是错误:

致命错误:在展开可选值时意外发现nil

这是代码

来自GitHub的类引用: 塔巴控制器 在这里,我添加了自定义按钮并调用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)
    }
}
protocol FirstViewControllerDelegate {
    func messageData(greeting: String)
}

class FirstViewController: UIViewController{
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    func sendData() {
        self.delegate?.messageData(greeting: “Hello SecondViewController”)
    }
}
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
    }
}
pvc.delegate = svc
FirstViewController 我想从这里向SecondViewController发送数据

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)
    }
}
protocol FirstViewControllerDelegate {
    func messageData(greeting: String)
}

class FirstViewController: UIViewController{
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    func sendData() {
        self.delegate?.messageData(greeting: “Hello SecondViewController”)
    }
}
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
    }
}
pvc.delegate = svc
第二视图控制器 这里我想接收从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)
    }
}
protocol FirstViewControllerDelegate {
    func messageData(greeting: String)
}

class FirstViewController: UIViewController{
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    func sendData() {
        self.delegate?.messageData(greeting: “Hello SecondViewController”)
    }
}
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
    }
}
pvc.delegate = svc
知道为什么在SecondViewController中出现错误吗?如果标签在SecondViewController中声明,为什么标签不可访问? 理想情况下,我希望能够调用方法onRaisedButton\uSender:UIButton!直接从SecondViewController,但无需子类RaisedTabBarController。我不知道这是否能解决错误,但我认为这会使我的代码更干净

编辑:2017年6月19日-已解决 我想要的效果可以直接在XCode中,在故事板中实现。我停止使用第三方类RaisedTabBarController,问题解决了。

这似乎是错误的

pvc.delegate = SecondViewController() as FirstViewControllerDelegate
尝试实例化第二个ViewController,就像在故事板中对第一个进行实例化一样

let svc = storyboard?.instantiateViewController(withIdentifier: “secondStoryBoardID”) as! SecondViewController
然后将代理设置为SecondViewController

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)
    }
}
protocol FirstViewControllerDelegate {
    func messageData(greeting: String)
}

class FirstViewController: UIViewController{
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    func sendData() {
        self.delegate?.messageData(greeting: “Hello SecondViewController”)
    }
}
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
    }
}
pvc.delegate = svc

你们初始化了第二视图控制器吗?我不确定我应该在哪里初始化它。仅供参考-如果协议调用直接从FirstViewController进行,则在FirstViewController和SecondViewController之间传递数据有效。确保SecondViewController符合协议。好的,从TabBarController我想转到FirstVC而不是SecondVC。仅供参考-我正在从FirstVC获取我在SecondVC中期望的数据,我只是得到一个关于标签为零的错误。您可以尝试检查插座是否在故事板中正确连接。否则,在视图中将出现一个断点方法,并查看您设置的其他插座是否为零,如果所有插座均为零或self为零,则您必须从故事板实例化控制器,如我所述。