繁体   English   中英

展开Segue后,新数据未更新

[英]New Data Not Updating after Unwind Segue Performed

我对Swift还是比较陌生,并且在解决这个问题上很困惑,这个问题很可能很容易解决,我只是想不通并继续撞墙。

我正在开发一个应用程序,其中用户在集合视图控制器中点击图像,然后执行退序返回主视图控制器,并使用用户选择的图像更新uiimageview。 我使放开序列起作用,但问题是uiimageview始终落后一个图像(即,我选择图像A,放回序列,并且没有图像显示,因此我选择图像B放回序列,然后选择了原始图像第一次,图像A在uiimageview中)。

这是目标视图控制器(我希望uiimageview基于集合视图中选择的图像进行更新的主视图控制器)的代码...

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

在集合视图控制器上,用户选择图像并向后退,我只按Ctrl并拖动该单元以在情节提要板上“退出”,然后选择unwindToVC segue。

就我的收藏视图而言,这是我的设置方式,因为我怀疑它与它的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
    }

抱歉,如果我发布了太多代码,但这是我的第一篇文章,就像我说我是开发新手一样,所以我认为最好包括一些额外的东西,而不要添加一些不需要的东西。

任何意见是极大的赞赏!

非常感谢!!! :)

之所以发生这种情况,是因为触发segue之后会调用collectionView:didSelectItemAtIndexPath: 因此,它始终是落后者。

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

有线程序出口

在“ 文档大纲”视图中找到该"myExitSegue" ,并在右侧的"myExitSegue" 属性”检查器中为其指定标识符,例如"myExitSegue" 然后在didSelectItemAtIndexPath ,最后要做的是调用performSegueWithIdentifier("myExitSegue", sender: self)

这就是我要做的:

删除源VC展开操作中的代码,以实现一个空的展开过程,如下所示(用于显示新图像的逻辑将放入集合VC中):

@IBAction func unwindToVC(segue: UIStoryboardSegue) {
}

为展开的搜索提供一个字符串标识符,以便您可以通过编程方式自动执行搜索。 在这里,我只是称我为“ Unwind”。

在此处输入图片说明 在此处输入图片说明

然后,在FrancoCollectionCollectionViewController内部放置一个属性,以保存所选的UIImage:

var selectedFranco: UIImage!

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

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

然后,我想补充的prepareForSegue在你的方法FrancoCollectionCollectionViewController到源视图控制器的图像设置为所选的图片:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM