简体   繁体   中英

Swift 5 - Issues With Passing Data From Class To Class

As an exercise to learn Swift, I'm creating a simple app where you use ImagePickerController to select a photo and get data about the photo. For now, I'm just pulling pixelWidth and pixelHeight data from photo's PHAsset.

My Setup: I have a ViewController class which I created using Storyboard that includes the UI, ImagePickerController and it's delegate which after selecting photo, will update data in another class called TestGlobalData .

The problem I'm running into is that while I'm able to update variables from ViewController to TestGlobalData , I can't get it to update back on ViewController

Here is my code. Any help would be appreciated, I'm totally stumped (As mentioned I'm also new to Swift, so pointing out any fundamental things I'm not getting would be appreciated too! )

// TestViewController.swift

class TestViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
  @IBOutlet weak var testPhotoView: UIImageView!
  @IBOutlet weak var testWidthLabel: UILabel!
  @IBOutlet weak var testHeightLabel: UILabel!
  var testWidthText: String?
  var testHeightText: String?
  var selectionFromPicker: UIImage?

  override func viewDidLoad() {
      super.viewDidLoad()
  }

  // Get imagePickerController ///////////////////////////////////////////////////////////////////

  @IBAction func getPhotoButton(_ sender: Any) {
      getImagePicker()
  }

  func getImagePicker() {
    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self
    imagePickerController.sourceType = .photoLibrary
    imagePickerController.allowsEditing = false
    present (imagePickerController, animated: true, completion: nil)
  }

  func imagePickerController(_ picker: UIImagePickerController,
                             didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    defer { dismiss (animated: true, completion: nil)}
    guard let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else { return }
    guard let asset = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset else { return }
    selectionFromPicker = selectedImage
    let data = TestGlobalData()
    data.testData = asset   // Updates PHAsset
    data.updateData()       // Data shows as updated here
    data.pushData()         // Data shows as updated here too
    self.updateTestPhoto()  // Photo updates successfully (photo does not get passed)
    self.textToLabel()      // Assigns text to UILobel
    self.checkData()        // Data is lost and shows as nil here
  }

  // Functions  //////////////////////////////////////////////////////////////////////////////

  // Assign Text To Label
  func textToLabel() {
      testWidthLabel.text = testWidthText
      testHeightLabel.text = testHeightText
  }

  // Update  Photo To Selected
  func updateTestPhoto() {
      testPhotoView.image = selectionFromPicker
  }

  // Final Check 

// TestGlobalData.swift

class TestGlobalData {

  var testData: PHAsset?
  var testWidth = Int()
  var testHeight = Int()
  var widthInString = String()
  var heightInString = String()

  func updateData() {
      testWidth = testData!.pixelWidth
      testHeight = testData!.pixelHeight
      widthInString = String(testWidth)
      heightInString = String(testHeight)
      //widthInString and testWidth updated successfully at this point
  }

  func pushData() {
      let vc = TestViewController()
      vc.testWidthText = widthInString
      vc.testHeightText = heightInString
      //vc.testWidthText show as updated successfully here
  }
}

The problem is you are creating a new instance of the TestViewController in the TestGlobalData class, specifically in the pushData() function.

Instead change the pushData to:

func pushData(vc: UIViewController) {
  vc.testWidthText = widthInString
  vc.testHeightText = heightInString
}

and change when you call the method as well to:

data.pushData(self)  

Here is some additional resources that should help you understand everything better :)

https://code.tutsplus.com/tutorials/swift-from-scratch-an-introduction-to-classes-and-structures--cms-23197 https://www.python-course.eu/python3_class_and_instance_attributes.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM