如何正确地将数据从一个Tab转换到另一个Tab

Sam*_*gNY 12 uitabbarcontroller ios segue uistoryboardsegue unwind-segue

故事板图像

我有一个标签栏控制器,它是初始视图控制器,PFLoginViewController如果用户没有登录,它也有一个小工具.登录/注册流程正常.

这两个标签是1.a UICollectionView,我IntroVC将从现在开始参考2. UITableView我将称之为FeedVC

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

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

所述SelectVC显示相片,并有一个UIButton,用户可以按下触发一个显示赛格瑞身心SEGUE到图像推入FeedVC.我创建Unwind segue的原因是因为没有它,图像会进入FeedVC(第二个选项卡),但第一个选项卡仍然会突出显示.

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

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

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.
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激!

相关代码如下.

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]
        }
}
Run Code Online (Sandbox Code Playgroud)

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)
        })
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Sur*_*gch 5

选择带有数据的另一个标签

此解决方案使用展开搜索切换到新选项卡并发送数据。

在此处输入图片说明

  • 绿色是标签1
  • 蓝色是选项卡1的后代
  • 黄色是标签2

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

Blue View Controller(选项卡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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

黄色视图控制器(表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
    }
}
Run Code Online (Sandbox Code Playgroud)

界面生成器

控制从按钮拖到源视图控制器顶部的“退出”图标。由于我们已经将展开的序列代码添加到目标View Controller中,因此它将作为选项显示。选择它。

在此处输入图片说明

笔记

  • 多亏了这个答案,我才能走上正确的道路。
  • 您还可以通过将控件从“退出”图标拖到“视图控制器”图标上来设置展开顺序。然后,您可以通过编程方式调用segue。有关详细信息,请参见此答案
  • Tab 1视图控制器没有要显示的特殊代码。


小智 0

我认为你的问题出在这一行(prepareForSegue方法)

self.navigationController?.popViewControllerAnimated(true)
Run Code Online (Sandbox Code Playgroud)

因为您试图在呈现 SelectVC 之前从堆栈中弹出视图控制器。这很可能是导航栏可能损坏的原因(如果弹出根视图控制器怎么办?)。您可以尝试以下方法:

self.navigationController?.popToRootViewControllerAnimated(true)
Run Code Online (Sandbox Code Playgroud)