本文介绍了从另一个视图控制器更新/更改数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据模型

class DataImage {
    var userId: String
    var value: Double
    var photo: UIImage?
    var croppedPhoto: UIImage?

init(userId:String, value: Double, photo: UIImage?, croppedPhoto: UIImage?){
    self.userId = userId
    self.value = value
    self.photo = photo
    self.photo = croppedPhoto
   }

}

ViewController(tableView)

var photos = [DKAsset]() //image source
var datas = [DataImage]()
override func viewDidLoad() {
    super.viewDidLoad()
        self.loadDataPhoto()
}

func loadDataPhoto(){
  var counter = 0
    for asset in photos{
        asset.fetchOriginalImageWithCompleteBlock({ image, info in // move image from photos to datas

            let images = image
            let data1 = dataImage(userId: "img\(counter+1)", value: 1.0, photo: images, croppedPhoto: images)
            self.datas += [data1]
            counter++

        })
    }
}

根据该代码,假设我有 5 个数据:

from that code, let's say i have 5 datas:

  • dataImage(userId: "img1", value: 1.0, photo: images,croppedPhoto:图片)
  • dataImage(userId: "img2", value: 1.0, photo: images,croppedPhoto:图片)
  • dataImage(userId: "img3", value: 1.0, photo: images, croppedPhoto:图片)
  • dataImage(userId: "img4", value: 1.0, photo: images,croppedPhoto:图片)
  • dataImage(userId: "img5", value: 1.0, photo: images,croppedPhoto:图片)

我从视图控制器中选择img3"并成功将其移动到cropviewController

CropViewController

var datasCrop: dataImage?
override func viewDidLoad() {
    super.viewDidLoad()
    if let datasCrop = datasCrop{
        // beforeImage.image = datasCrop.photo
        photoId.text = datasCrop.userId
        self.cropView.imageToCrop = datasCrop.photo

    }
}

@IBAction func handleCropButton(sender: UIButton)
{
    if let croppedImage = cropView.croppedImage()
    {
      croppedImage //theCropped img3's cropped image
    }
 }

croppedViewController中,我裁剪了img3"图像.

in croppedViewController, i crop the "img3" image.

我的问题是,我希望在 datas["img3"] 中更新裁剪后的图像并将其移回 viewController.如何做到这一点?

My question is, I want that cropped image to be update in datas["img3"] and move it back to viewController. how to do this?

推荐答案

看,你有数组 datas.您可以执行以下操作:

Look, you have the array datas. You can do the next:

var datas = [DataImage]()
class ViewController: UIViewController {
    override func viewDidLoad() {
        // fill your array with append or addObject
        print(datas) // without "self."
}

这篇关于从另一个视图控制器更新/更改数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-19 23:33