简体   繁体   English

展开Segue后,新数据未更新

[英]New Data Not Updating after Unwind Segue Performed

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

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. 我正在开发一个应用程序,其中用户在集合视图控制器中点击图像,然后执行退序返回主视图控制器,并使用用户选择的图像更新uiimageview。 I got the unwind segue to work but the problem is that the uiimageview is always one image behind (ie 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始终落后一个图像(即,我选择图像A,放回序列,并且没有图像显示,因此我选择图像B放回序列,然后选择了原始图像第一次,图像A在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)... 这是目标视图控制器(我希望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)
    }

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

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

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!!! 非常感谢!!! :) :)

This is happening because collectionView:didSelectItemAtIndexPath: is called after the segue is triggered. 之所以发生这种情况,是因为触发segue之后会调用collectionView:didSelectItemAtIndexPath: So, it is always one behind. 因此,它始终是落后者。

One way to fix this is to call the unwind segue programmatically with performSegueWithIdentifier instead of wiring it from the cell to the exit. 解决此问题的一种方法是使用performSegueWithIdentifier以编程方式调用展开序列 ,而不是将其从单元连接到出口。 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). 为此,请将退出序列viewController顶部的viewController图标(最左侧的图标)连接到退出图标 (最右侧的图标)。

有线程序出口

Find this segue in the Document Outline view and give it an identifier such as "myExitSegue" in the Attributes Inspector on the right. 在“ 文档大纲”视图中找到该"myExitSegue" ,并在右侧的"myExitSegue" 属性”检查器中为其指定标识符,例如"myExitSegue" Then in didSelectItemAtIndexPath , the last thing to do is to call performSegueWithIdentifier("myExitSegue", sender: self) . 然后在didSelectItemAtIndexPath ,最后要做的是调用performSegueWithIdentifier("myExitSegue", sender: self)

Here's what I would do: 这就是我要做的:

Remove the code in the source VC unwind action for an empty unwind segue like so (the logic for displaying the new image will be put in the collection VC): 删除源VC展开操作中的代码,以实现一个空的展开过程,如下所示(用于显示新图像的逻辑将放入集合VC中):

@IBAction func unwindToVC(segue: UIStoryboardSegue) {
}

Give your unwind segue a string identifier so that you may perform the segue programmatically not automatically. 为展开的搜索提供一个字符串标识符,以便您可以通过编程方式自动执行搜索。 Here, I just called mine "Unwind". 在这里,我只是称我为“ Unwind”。

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

Then, inside of FrancoCollectionCollectionViewController put a property to hold the selected UIImage: 然后,在FrancoCollectionCollectionViewController内部放置一个属性,以保存所选的UIImage:

var selectedFranco: UIImage!

Within didSelectItemAtIndexPath set the property to the selected image and then manually perform the segue: didSelectItemAtIndexPath将属性设置为所选图像,然后手动执行segue:

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

Then I would add the prepareForSegue method in your FrancoCollectionCollectionViewController to set the source view controller's image to the selected image: 然后,我想补充的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