从另一个视图控制器更新/更改数组值

时间:2016-01-21 04:24:02

标签: ios swift tableview

数据模型

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个数据:

  • dataImage(userId:“img1”,值:1.0,照片:images,croppedPhoto:    图像)
  • dataImage(userId:“img2”,值:1.0,照片:images,croppedPhoto:    图像)
  • dataImage(userId:“img3”,值:1.0,照片:图片, croppedPhoto:    图片
  • dataImage(userId:“img4”,值:1.0,照片:images,croppedPhoto:    图像)
  • dataImage(userId:“img5”,值:1.0,照片: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”图片。

我的问题是,我希望裁剪的图像在数据[“img3”]中更新,然后将其移回 viewController 。怎么做?

2 个答案:

答案 0 :(得分:1)

看,你有阵列datas。你可以做下一个:

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

答案 1 :(得分:0)

您可以使用NSNotification或委托方法在这两个viewcontrollers之间传递信息 试试这个answer