执行 Unwind Segue 后新数据未更新

New Data Not Updating after Unwind Segue Performed

我是 Swift 的新手,遇到这个很可能很容易解决的问题时遇到了麻烦,我就是想不通,一直碰壁。

我正在制作一个应用程序,用户在 collection 视图控制器中点击图像,它会执行展开转场回到主视图控制器,并使用用户 [=] 的图像更新 uiimageview 34=]编辑。我让 unwind segue 工作但问题是 uiimageview 总是落后一个图像(即我 select image A,unwind segue back 并且没有图像显示,所以我 select image B unwind segue back然后是我第一次 selected 的原始图像,图像 A,在 uiimageview 中)。

这是目标视图控制器的代码(我希望 uiimageview 根据 select 在 collection 视图中编辑的图像进行更新的主视图控制器)...

 @IBAction func unwindToVC(segue:UIStoryboardSegue) {
    if(segue.sourceViewController .isKindOfClass(FrancoCollectionCollectionViewController))
    {
        let francoCollectionView:FrancoCollectionCollectionViewController = segue.sourceViewController as! FrancoCollectionCollectionViewController
        let selectedImageFromLibrary = selectedFranco
        dragImage!.image = UIImage(named: selectedImageFromLibrary)
    }

在 collection 视图控制器上,用户 select 将图像放回去,我只是 ctrl + 将单元格拖到情节提要上的 "exit" 和 select编辑了 unwindToVC 转场。

就我的 collection 观点而言,我是如何设置的,因为我怀疑它与它的 didSelectItemAtIndexPath 部分有关,但我不确定...

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    return francoPhotos.count 
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! FrancoCellCollectionViewCell

    // sets each cell of collection view to be a uiimage view of francoPhotos

    let image = UIImage(named: francoPhotos[indexPath.row])
    cell.francoImageCell.image = image

    return cell
}

    // highlights cell when touched 
    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        selectedFranco = francoPhotos[indexPath.row]
        let cell = collectionView.cellForItemAtIndexPath(indexPath)
        cell!.layer.borderWidth = 3.0
        cell!.layer.borderColor = UIColor.blueColor().CGColor

}
    override func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
        let cell = collectionView.cellForItemAtIndexPath(indexPath)
        cell!.layer.borderWidth = 0.0
        cell!.layer.borderColor = UIColor.clearColor().CGColor
    }

抱歉,如果我 post 编写了太多代码,但这是我的第一个 post,就像我说的,我是开发新手,所以我认为最好包含一些代码额外的而不是没有需要的东西。

非常感谢任何建议!

非常感谢!!! :)

发生这种情况是因为 collectionView:didSelectItemAtIndexPath: 在触发 segue 后被调用。所以,总是落后一分。

解决此问题的一种方法是使用 performSegueWithIdentifier 以编程方式调用 unwind segue,而不是将其从单元连接到出口。为此,请将 viewController 顶部的 viewController 图标(最左边的图标)连接到 退出图标(最右边的图标)。

Document Outline 视图中找到这个 segue 并给它一个 identifier 例如 "myExitSegue" in the [=24] =]右边的Attributes Inspector。然后在didSelectItemAtIndexPath中,最后要做的就是调用performSegueWithIdentifier("myExitSegue", sender: self).

我会这样做:

删除源代码VC unwind action for an empty unwind segue 像这样(显示新图像的逻辑将放在集合VC):

@IBAction func unwindToVC(segue: UIStoryboardSegue) {
}

为您的展开转场提供一个字符串标识符,这样您就可以通过编程方式执行转场,而不是自动进行。在这里,我刚刚调用了我的 "Unwind".

然后,在FrancoCollectionCollectionViewController里面放一个属性来保存选中的UIImage:

var selectedFranco: UIImage!

didSelectItemAtIndexPath 内将 属性 设置为所选图像,然后手动执行转场:

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    // ... 
    self.selectedFranco = francoPhotos[indexPath.row]
    self.performSegueWithIdentifier("Unwind", sender: self)
}

然后我会在您的 FrancoCollectionCollectionViewController 中添加 prepareForSegue 方法以将源视图控制器的图像设置为所选图像:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "Unwind" {
        let sourceVC = segue.destinationViewController as! YourSourceViewController
        sourceVC.dragImage.image = selectedImageView.image
    }
}