简体   繁体   English

使用标签和PerformSegueWithIdentifier通过tabBar DidSelectItem传递数据

[英]Passing data with tabBar DidSelectItem using tags and PerformSegueWithIdentifier

I am using swift 3 have a taBbar and when I select an tabBarItem the segue is called programatically to go to a new View Controller. 我正在使用swift 3具有taBbar,当我选择tabBarItem时,将以编程方式调用segue以转到新的View Controller。 I also need to pass some data with this segue. 我还需要通过此segue传递一些数据。

my code (for tabbar didSelectItem): 我的代码(用于标签栏didSelectItem):

 func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        if item.tag == 0 {
            self.performSegueWithIdentifier("nearbyHotelsSegue", sender: nil)
        } else if item.tag == 1 {
            self.performSegueWithIdentifier( "nearbyRestaurantsSegue", sender: nil)
        } else if item.tag == 2 {
            self.performSegueWithIdentifier( "nearbyEventsSegue", sender: nil)
        } else if item.tag == 3 {
            self.performSegueWithIdentifier( "morePlacesSegue", sender: nil)
        } 
    }

From my research so far, I know that can use PrepareForSegue for this, but I can't use this inside the tabBar (didSelectItem) method. 到目前为止,根据我的研究,我知道可以为此使用PrepareForSegue,但是不能在tabBar(didSelectItem)方法中使用它。 How can I pass data using tabbar didSelectitem method or is there any other good way to achieve this? 如何使用选项卡的didSelectitem方法传递数据,或者有没有其他好的方法来实现此目的? Thanks. 谢谢。

The prepareForSegue method is used as an overridden one, so you should use it out of tabBar function's bounds. prepareForSegue方法用作重写的方法,因此应在tabBar函数的范围之外使用它。 When you call performSegue(withIdentifier: "segueName", sender: nil) , you have given nil in sender. 当您调用performSegue(withIdentifier: "segueName", sender: nil) ,您在发送者中给出了nil Sender is the parameter of type Any? Sender是类型为Any?的参数Any? , where you put your custom data you want to pass to another controller. ,您将自定义数据传递到另一个控制器的位置。 In prepare(forSegue: UIStoryboardSegue, sender: Any?) when you cast your destination view controller as the one you want to use, pass the sender data to this controller, eg: 当您将目标视图控制器转换为要使用的控制器时prepare(forSegue: UIStoryboardSegue, sender: Any?)prepare(forSegue: UIStoryboardSegue, sender: Any?) ,将发送方数据传递给此控制器,例如:

override func prepare(forSegue: UIStoryboardSegue, sender: Any?) {
if let controller = segue.destination as? YourDestinationViewController {
controller.receivedData = sender
}
}

I solved this using the following code. 我使用以下代码解决了这个问题。 it seems prepareForSegue is not effected by the use of PerformSegueWithIdentifier to perform the segue programmatically. 似乎prepareForSegue不受使用PerformSegueWithIdentifier来以编程方式执行segue的影响。
So I used the code posted in the question unchanged and then added the following code to pass data. 因此,我使用了在问题中张贴的代码不变,然后添加了以下代码来传递数据。

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "nearbyHotelsSegue" {

            if let toViewController = segue.destinationViewController as? NearbyHotelsViewController {
                toViewController.returnedText = (searchBaseItem?.baseItemId)!
            }
        }

//....other ifs
}

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

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