简体   繁体   English

如何从标签栏控制器执行show segue?

[英]How to perform show segue from tab bar controller?

I have a tab bar controller with 2 views attached, each embed in navigation controller. 我有一个带有2个视图的标签栏控制器,每个视图都嵌入在导航控制器中。 I've made a segue kind show from tab bar controller to another view controller with identifier "toNew". 我从标签栏控制器到另一个标识符为“ toNew”的视图控制器进行了segue种显示。 In one of the tab bar views I have a button which should trigger this segue with identifier "toNew". 在一个标签栏视图中,我有一个按钮,该按钮应使用标识符“ toNew”触发此segue。 I tried DataDelegate but it doesn't work here. 我尝试了DataDelegate,但在这里不起作用。

It's this part of storyboard 这是情节提要的一部分

This is view controller file for view controller attached in tab bar 这是标签栏中附加的用于视图控制器的视图控制器文件

import UIKit

protocol DataDelegate {
func sendData(data : String)
}

class NavContToNew: UIViewController , UITabBarDelegate {

var delegate : DataDelegate?
var data : String = "ToNew"

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func dismiss(_ sender: Any) {
    //self.dismiss(animated: true, completion: nil)
    self.delegate?.sendData(data:self.data)
    print("Perform segue delegate")
}
}

And this is tab bar controller.swift file 这是标签栏controller.swift文件

import UIKit

class TabBarCont: UITabBarController , DataDelegate {


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func sendData(data: String) {
    if data == "ToNew" {
        print("Segue perform")
        self.performSegue(withIdentifier: "toNew", sender: self)
    }
}
}

In you NavContToNew , you haven't assigned a delegate. 在NavContToNew中,您尚未分配委托。

@IBAction func dismiss(_ sender: Any) {
            self.delegate = self.tabBarController as! TabBarCont
    //self.dismiss(animated: true, completion: nil)
    self.delegate?.sendData(data:self.data)
    print("Perform segue delegate")
}

By the way , you don't need to use delegate. 顺便说一句,您不需要使用委托。 You can call your taBarController anywhere in its viewControllers. 您可以在其viewControllers中的任何位置调用taBarController。 Here, you can call it in NavContToNew without using delegate. 在这里,您可以在NavContToNew中调用它,而无需使用委托。

self.tabBarController?.performSegue(withIdentifier: "toNew", sender: self.tabBarController)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM