ios - 如何将图像从桌面 View 传输到第二个 View

标签 ios swift

我有一个表格 View ,每个单元格中都有一个图像。当用户选择单元格时,我想将该图像传递给第二个 View Controller 。我设置了从单元格到第二个 View Controller 的 Segue。

但是,我的代码在第二个 View Controller 中为图像生成 nil。

我确信我应该为此使用委托(delegate),并且我发现了很多将它们传递回第一个 View Controller 的示例,但不是这种方式。我不知道如何设置它。

谁能给我举个例子吗?

TableView(第一个 View Controller )

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "imageCropSegue"{
        if let destination = segue.destinationViewController as? cropImageView {
            if let profileIndex = self.parseTable.indexPathForSelectedRow()?.row {
                var index = NSIndexPath(forRow: profileIndex, inSection: 0)

                var cell = self.parseTable.cellForRowAtIndexPath(index) as! TableViewCell
                destination.cropImage.image = cell.cellImage.image //throws an exception as destination.cropImage = nil
            }
        }

    }
}

第二个 View Controller

class cropImageView:UIViewController{
    @IBOutlet weak var cropImage: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()

    }
} 

最佳答案

第二个 View Controller :

class cropImageView:UIViewController{
    @IBOutlet weak var cropImage: UIImageView!
    var selectedImage : UIImage?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.cropImage.image = self.selectedImage //Assign selected Image here

    }
} 
FirstView Controller 中的

PrepareForSegue 方法如下:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "imageCropSegue"{
        if let destination = segue.destinationViewController as? cropImageView {
            if let profileIndex = self.parseTable.indexPathForSelectedRow()?.row {
                var index = NSIndexPath(forRow: profileIndex, inSection: 0)

                var cell = self.parseTable.cellForRowAtIndexPath(index) as! TableViewCell
                //destination.cropImage.image = cell.cellImage.image //Change here

               destination.selectedImage= cell.cellImage.image
            }
        }

    }
}

关于ios - 如何将图像从桌面 View 传输到第二个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31333358/

相关文章:

ios - 不同iOS版本之间的应用程序迁移

ios - UIActivityIndi​​catorView核心数据后台上下文获取

swift - 绕过 Firebase 创建的唯一自动 ID。 swift 3.0

ios - 访问所有firebase用户的信息

ios - GCD 通过cancellable_dispatch_after 停止

arrays - 在 Swift+Parse 中根据用户距离形式的 field 位置填充 tableView

iphone - 同一个 UITextField 中的居中占位符,但大小不同

ios - 从 AppDelegate 呈现 UIAlertController

iphone - 即使使用 performSelectorOnMainThread 和 waitUntilDone :NO,UI 也会锁定

ios - RxSwift 计算变量由 2 个其他变量组合而成