3

Data Model

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++

        })
    }
}

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

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

and i choose "img3" from view controller and successfully move it to 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
    }
 }

in croppedViewController, i crop the "img3" image.

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

Cristik
  • 30,989
  • 25
  • 91
  • 127
Aldo Lazuardi
  • 1,898
  • 2
  • 19
  • 34
  • I am lazy to type the answer :D. But you can review this link http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers. It tells you how to pass object between view controller – t4nhpt Jan 21 '16 at 04:58
  • do you have any swift source? @t4nhpt – Aldo Lazuardi Jan 21 '16 at 05:08
  • Please see this link for Swift: http://stackoverflow.com/questions/25589240/trying-to-understand-protocol-delegates-in-swift – t4nhpt Jan 21 '16 at 05:32
  • You can try to make your array global and access to it from everywhere or you can access to it with `ViewController().yourArray` – iamalizade Jan 21 '16 at 06:28
  • @iamalizade oh so i could use ViewController().datas in 'handleCropButton'? im going to try it now – Aldo Lazuardi Jan 21 '16 at 06:32
  • @AldoLazuardi let me know what you had. In case of success I'll transfer my comment to answer – iamalizade Jan 21 '16 at 06:35
  • @iamalizade you mean globally so i set public `var datas= [dataPhoto]()` in **viewcontroller** and put `ViewController().datas[index!].photo = croppedPhoto` in **cropviewcontroller** right? it shows "fatal error: Array index out of range" – Aldo Lazuardi Jan 21 '16 at 07:00
  • @AldoLazuardi, no, I meant you take your array and paste it above the class, like `var datas = [...] class ViewController` – iamalizade Jan 21 '16 at 07:07
  • @iamalizade im sorry i kinda lost, could you explain it in answer? after i took `var datas= [dataPhoto]()` above viewController(), now datas inside view controller shows "view controller has no ember of datas" – Aldo Lazuardi Jan 21 '16 at 07:17

2 Answers2

1

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."
}
iamalizade
  • 228
  • 1
  • 5
0

You can use NSNotification or delegate methods to pass information between those two viewcontrollers Try this answer

Community
  • 1
  • 1
Devanshu Saini
  • 765
  • 5
  • 24