【问题标题】:New Data Not Updating after Unwind Segue Performed执行 Unwind Segue 后新数据未更新
【发布时间】:2015-11-30 20:02:05
【问题描述】:

我对 Swift 比较陌生,遇到了这个问题,这个问题可能很容易解决,我就是想不通,一直碰壁。

我正在制作一个应用程序,其中用户在集合视图控制器中点击图像,它会执行展开转回主视图控制器并使用用户选择的图像更新 uiimageview。我得到了展开 segue 的工作,但问题是 uiimageview 总是在后面一个图像(即我选择图像 A,展开 segue 并且没有图像显示,所以我选择图像 B 展开 segue 然后我选择了原始图像第一次,图像 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
    }

抱歉,如果我发布了太多代码,但这是我的第一篇文章,就像我说我是开发新手一样,所以我认为包含一些额外的东西比没有需要的东西更好。

非常感谢任何建议!

非常感谢!!! :)

【问题讨论】:

  • 不确定这是否会有所帮助,因为这对于 SO 来说是一个很大的问题。解决这个问题可能需要做很多工作。但是在展开之后,如何在collectionView 上重新加载呢?因此,在 viewwillappear 或 viewdidappear 或 viewdidlayoutsubviews 或其他一些自定义方法中,您调用在 collectionview 中重新加载
  • 凯特,当你说你碰壁时,具体是什么问题?

标签: ios swift uiimageview uicollectionview unwind-segue


【解决方案1】:

这是因为 collectionView:didSelectItemAtIndexPath: 在 segue 被触发后被调用。所以,它总是落后一个。

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

Document Outline 视图中找到这个 segue,并在右侧的 Attributes Inspector 中为其指定一个 标识符,例如 "myExitSegue"。然后在didSelectItemAtIndexPath,最后就是调用performSegueWithIdentifier("myExitSegue", sender: self)

【讨论】:

    【解决方案2】:

    我会这样做:

    删除源 VC 展开动作中的代码,用于空展开 segue,如下所示(显示新图像的逻辑将放在集合 VC 中):

    @IBAction func unwindToVC(segue: UIStoryboardSegue) {
    }
    

    给你的 unwind segue 一个字符串标识符,这样你就可以不自动地以编程方式执行 segue。在这里,我只是叫我的“放松”。

    然后,在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)
    }
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-20
      • 1970-01-01
      相关资源
      最近更新 更多