如何正确地将数据从一个Tab转移到另一个Tab [英] How to segue with data from one Tab to another Tab properly

查看:247
本文介绍了如何正确地将数据从一个Tab转移到另一个Tab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标签条形码控制器,它是初始视图控制器,它还有一个 PFLoginViewController ,如果用户没有登录,它会瘫痪。登录/注册流程正常。

I have a Tab Bar Controller which is the Initial View Controller, which also has a PFLoginViewController that pups up if a user isn't logged in. The Login/Signup flow works fine.

这两个标签是
1.一个 UICollectionView 我将其称为 IntroVC 从现在起
2.一个 UITableView 我将其称为 FeedVC

The two tabs are 1. a UICollectionView which I will refer to as IntroVC from now on 2. a UITableView which I will refer to as FeedVC

当用户点击 IntroVC 中的照片时,会触发Show segue(通过 prepareForSegue )显示第3个屏幕( UIView ),这在技术上不是标签。从现在开始,我将把它称为 SelectVC

When a user clicks a photo in IntroVC, a Show segue is triggered (via prepareForSegue) that shows a 3rd screen (UIView) which is technically not a tab. I will refer to this as the SelectVC from now on.

注意:所有这些屏幕也都嵌入(ded)在导航控制器中。

NOTE: All of these screens are also Embed(ded) In a Navigation Controller.

SelectVC 显示一张照片,并且有 UIButton 用户可以按下哪个触发显示segue和Unwind segue,将图像推送到 FeedVC 。我创建Unwind segue的原因是因为没有它,图像会进入 FeedVC (第二个标签),但第一个标签仍会突出显示。

The SelectVC displays a photo, and there is a UIButton that the user can press which triggers a Show segue and Unwind segue, to push the image into the FeedVC. The reason I created an Unwind segue is because without it, the image would push into the FeedVC (2nd tab) but the first tab would still be highlighted.

我用Unwind segue解决了这个问题,但是我注意到我遇到了一个问题,在选择之后,当我按下第一个标签(Intro VC)时,导航栏有一个后退按钮,我使用 SelectVC 按钮推送图像的次数越多,我在 IntroVC中按回的次数就越多。我对如何解决这个问题感到非常困惑。很明显,我没有正确地连接流程,似乎多次生成 IntroVC

I fixed this with the Unwind segue, but I noticed I'm having a problem where after a selection, when I press the 1st tab (Intro VC) the Nav bar has a Back button, and the more times I use the SelectVC button to push images, the more times I have to press Back in the IntroVC. I'm very confused about how to fix this. It's apparent that I'm not hooking up the flow properly and it seems that the IntroVC is being generated multiple times?

当我浏览模拟器中的segues时,我在控制台中收到以下消息:

I get the following message in the console when I go through the segues in Simulator:

Nested pop animation can result in corrupted navigation bar

Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.

任何帮助将不胜感激!

Any help would be greatly appreciated!

以下相关代码。

IntroVC.swift

IntroVC.swift

@IBAction func unwindToIntroView(segue: UIStoryboardSegue) {
    self.tabBarController!.selectedIndex = 1


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showFeedItem" {
        let selectScreenVC = segue.destinationViewController as! SelectScreenViewController
        let cell = sender as! UICollectionViewCell
        if let indexPath = self.collectionView!.indexPathForCell(cell) {
            self.navigationController?.popViewControllerAnimated(true)
            selectScreenVC.currentVenue = venueItems[indexPath.row]
        }
}

SelectVC.swift

SelectVC.swift

@IBAction func pushSelection(sender: UIButton) {
    var feedItem = FeedItem()
    if let currentItem = currentItem {
        feedItem.nName = currentItem.nName
        feedItem.imageFile = currentItem.lgImg
        feedItem.userName = PFUser.currentUser()!.username!
        feedItem.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
            self.performSegueWithIdentifier("unwindToVenueView", sender: self)
        })
    }
}

我知道这是奇怪的结构,如果我缺少信息,需要完全理解 - 请告诉我,我会相应地进行编辑。

I know this is weirdly structured, and if I'm missing information that is needed to fully understand - please let me know and I'll edit accordingly.

推荐答案

使用数据搜索另一个标签



此解决方案使用的是展开segue切换到新标签并发送数据。

Segue to another tab with data

This solution uses an unwind segue to switch to a new tab and send it data.


  • 绿色是标签1

  • 蓝色是标签1的后代

  • 黄色标签2

目标:从蓝色变为黄色并同时传递数据。

Goal: Go from Blue to Yellow and pass data at the same time.

蓝视图控制器(选项卡1的后代)

import UIKit
class BlueViewController: UIViewController {

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // this is the data that we want to send
        let myData = "Hello from Blue"

        // Get a reference to the destination View Controller
        // and set the data there.
        if let yellowVC = segue.destination as? YellowViewController {
            yellowVC.data = myData
        }
    }
}

黄色视图控制器(标签2)

import UIKit
class YellowViewController: UIViewController {

    var data: String?

    @IBOutlet weak var dataLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        updateUI()
    }

    @IBAction func comingFromBlueUnwindSegue(segue: UIStoryboardSegue) {
        // This may get called before the UI views have been loaded if
        // the user has not navigated to this tab before. So we need to make
        // sure that the label has been initialized. If it hasn't then we
        // will have our chance to call updateUI() in viewDidLoad().
        // We have to call it here too, though, becuase if the user has
        // previously navigated to this tab, then viewDidLoad() won't get
        // called again.
        if dataLabel != nil {
            updateUI()
        }
    }

    func updateUI() {
        // only update the label if the string data was previously set
        guard let myString = data else {return}
        dataLabel.text = myString
    }
}



界面生成器



控制从按钮拖动到源顶部的退出图标查看控制器。由于我们已将展开的segue代码添加到目标View Controller,因此它将显示为一个选项。选择它。

Interface Builder

Control drag from the button to the Exit icon on the top of the source View Controller. Since we have already added the unwind segue code to the destination View Controller, it will show up as an option. Choose it.


  • 感谢此答案让我走上正轨。

  • 您还可以通过控制从退出图标拖动到视图控制器图标来设置展开segue。然后你可以通过编程方式调用segue。有关详细信息,请参阅此答案

  • 没有特别代码可以显示选项卡1视图控制器。

  • Thanks to this answer for setting me on the right track.
  • You could also set the unwind segue by control dragging from the Exit icon to the View Controller icon. Then you could call the segue programmatically. See this answer for details.
  • There was no special code to show for the Tab 1 view controller.

这篇关于如何正确地将数据从一个Tab转移到另一个Tab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆