展开Segue后,新数据未更新 [英] New Data Not Updating after Unwind Segue Performed

查看:97
本文介绍了展开Segue后,新数据未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Swift还是比较陌生,并且正在解决这个可能很容易解决的问题,我只是无法弄清楚并不断撞墙.

I'm relatively new to Swift and am stumbling on this problem that is likely very simple to fix, I just can't figure it out and keep hitting a brick wall.

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

I'm making an app where a user taps an image in a collection view controller and it performs an unwind segue back to the main view controller and updates a uiimageview with the image that the user selected. I got the unwind segue to work but the problem is that the uiimageview is always one image behind (i.e. I select image A, unwind segue back and no image displays, so I select image B unwind segue back and then the original image I selected the first time, image A, is in the uiimageview).

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

Here is the code for the destination view controller (main view controller where I want the uiimageview to update based on the image selected in the collection view)...

 @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.

On the collection view controller where the user selects the image and it unwinds back, I just ctrl + dragged the cell to "exit" on the storyboard and selected the unwindToVC segue.

就我的收藏夹视图而言,这是我的设置方式,由于我有怀疑,它与其中的didSelectItemAtIndexPath部分有关,但我不确定...

As far as my collection view here is how I have that set up, since I have a suspiscion it's related to the didSelectItemAtIndexPath part of it but I'm not sure...

    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
    }

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

Sorry if I posted way too much code but this is my first post and like I said I'm new to development, so I figured it would be better to include a little extra rather than not have something that is needed.

任何建议都将不胜感激!

Any advice is greatly appreciated!

非常感谢!!! :)

Thanks so much!!! :)

推荐答案

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

This is happening because collectionView:didSelectItemAtIndexPath: is called after the segue is triggered. So, it is always one behind.

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

One way to fix this is to call the unwind segue programmatically with performSegueWithIdentifier instead of wiring it from the cell to the exit. To do this, wire your exit segue from the viewController icon (left most icon) at the top of your viewController to the exit icon (right most icon).

文档大纲视图中找到此序列,并在右侧的 Attributes Inspector 中为其提供一个标识符,例如"myExitSegue".然后在didSelectItemAtIndexPath中,最后要做的就是调用performSegueWithIdentifier("myExitSegue", sender: self).

Find this segue in the Document Outline view and give it an identifier such as "myExitSegue" in the Attributes Inspector on the right. Then in didSelectItemAtIndexPath, the last thing to do is to call performSegueWithIdentifier("myExitSegue", sender: self).

这篇关于展开Segue后,新数据未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆