12

故事板图像

PFLoginViewController我有一个选项卡栏控制器,它是初始视图控制器,如果用户未登录,它也有一个小狗。登录/注册流程工作正常。

这两个选项卡是 1. a从现在开始我将UICollectionView称为IntroVC2. aUITableView我将称为FeedVC

当用户单击 中的照片时IntroVC,会触发 Show segue(通过prepareForSegue),显示第三个屏幕(UIView),这在技术上不是选项卡。SelectVC从现在开始,我将其称为。

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

显示SelectVC一张照片,UIButton用户可以按下一个触发 Show segue 和 Unwind 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.

任何帮助将不胜感激!

相关代码如下。

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

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

4

3 回答 3

5

转到另一个带有数据的选项卡

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

在此处输入图像描述

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

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

代码

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

界面生成器

控制从按钮拖动到源视图控制器顶部的退出图标。由于我们已经将展开转场代码添加到目标视图控制器,它将显示为一个选项。选择它。

在此处输入图像描述

笔记

  • 感谢这个答案让我走上了正确的轨道。
  • 您还可以通过控制从 Exit 图标拖动到 View Controller 图标来设置 unwind segue。然后你可以以编程方式调用segue。有关详细信息,请参阅此答案
  • Tab 1 视图控制器没有要显示的特殊代码。
于 2017-12-11T11:28:48.797 回答
0

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

self.navigationController?.popViewControllerAnimated(true)

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

self.navigationController?.popToRootViewControllerAnimated(true)
于 2015-05-01T16:17:20.250 回答
0

我不确定我是否理解你self.navigationController?.popViewControllerAnimated(true)在 IntroVC.swift 的 prepareForSegue: 中调用的原因:这样你甚至在呈现之前就将 ViewController 从堆栈中“弹出”(这意味着你实际上试图将 IntroVC 从堆栈中弹出,而不是选择VC)。

我要尝试的第一件事是在 prepareForSegue: 中评论该行,然后看看会发生什么。

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

让我知道这个是否奏效!

祝你今天过得愉快,

@cdf1982

于 2015-05-03T10:33:25.433 回答