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

故事板图像

我有一个标签栏控制器,这是初始视图控制器,它也有一个PFLoginViewController如果用户没有login幼崽。login/注册stream程正常工作。

这两个选项卡是1.一个UICollectionView ,我将从现在开始将其称为IntroVC 2.一个UITableView ,我将引用FeedVC

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

注:所有这些屏幕也embedded(设备)在导航控制器。

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

我用Unwind segue修正了这个问题,但是我注意到在select后,当我按下第一个选项卡(Intro VC)时,导航栏有一个返回button,而且我使用SelectVCbutton的次数越多推动图像,我不得不在IntroVC按回来。 我很困惑如何解决这个问题。 很明显,我没有正确地连接stream程,似乎IntroVC正在多次生成?

当我在Simulator中通过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. 

任何帮助将不胜感激!

下面的相关代码。

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

 @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) }) } } 

我知道这是奇怪的结构,如果我错过了需要充分理解的信息 – 请让我知道,我会相应地编辑。

与另一个选项卡数据

这个解决scheme使用unwind segue来切换到一个新的标签并发送数据。

在这里输入图像说明

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

目标:从蓝色到黄色同时传递数据。

蓝色视图控制器(Tab 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 } } 

界面生成器

控制从button拖动到源视图控制器顶部的“退出”图标。 由于我们已经将展开的segue代码添加到目标View Controller,因此它将显示为一个选项。 select它。

在这里输入图像说明

笔记

  • 感谢这个答案让我走上正轨。
  • 您也可以通过控制从“退出”图标拖动到“视图控制器”图标来设置展开顺序。 然后你可以以编程方式调用segue。 看到这个答案的细节。
  • Tab 1视图控制器没有显示特殊的代码。

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

 self.navigationController?.popViewControllerAnimated(true) 

因为您在呈现SelectVC之前试图从堆栈中popup视图控制器。 这可能是导致一个可能损坏的导航栏(如果你popup根视图控制器?)的原因。 您可以尝试以下方法:

 self.navigationController?.popToRootViewControllerAnimated(true) 

我不确定我是否理解你在调用self.navigationController?.popViewControllerAnimated(true)的原因self.navigationController?.popViewControllerAnimated(true) :introVC.swift:这样你就可以在视图控制器出栈之前“popup”ViewController(这意味着你实际尝试将IntroVCpopup堆栈,而不是SelectVC)。

首先我会尝试,在prepareForSegue中注释该行:看看会发生什么。

我现在不能尝试,但如果在self.performSegueWithIdentifier("unwindToVenueView", sender: self)调用self.performSegueWithIdentifier("unwindToVenueView", sender: self)会自动触发自身的popup,而不需要手动调用它,我不会感到惊讶。 如果不是这样,可以添加popViewControllerAnimated (或者popToRootViewControllerAnimated )来pushSelection:SelectVC.swift。

让我知道这个是否奏效!

祝你今天愉快,

@ cdf1982