Home > other >  Swift: How should I transfer data to other class in this case?
Swift: How should I transfer data to other class in this case?

Time:06-09

I was designed to present the pop-up screen when the removeReview button is pressed.

The removeReview button is placed in ReviewItem.swift.

Right below is parts of the codes that is from ReviewItem.swift that I wrote. (The class type is the UITableViewCell)

weak var viewController: UIViewController?
var passwords: String = ""
var reviewNum: String = ""
@IBAction func removeReview(_ sender: Any) {
        
  print("ReviewItem - removeReview function called")
     
  print("Passwords: \(passwords)")
  print("Review_number: \(reviewNum)")
        
  let PopUpVC = PopUpVC()
  PopUpVC.pw = self.passwords
  PopUpVC.reviewDoc = self.reviewNum
        
  print(PopUpVC.pw)
  print(PopUpVC.reviewDoc)
        
  let storyboard = UIStoryboard.init(name: "PopUp", bundle: nil)
  let alertPopUpVC = storyboard.instantiateViewController(withIdentifier: "VerifyPasswords")
  alertPopUpVC.modalPresentationStyle = .overCurrentContext
  alertPopUpVC.modalTransitionStyle = .crossDissolve

  viewController?.present(alertPopUpVC, animated: true, completion: nil)
        
}

Though in the beginning of the code, I initialized the passwords and reviewNum as "", in the other swift file, I wrote the code

let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ReviewItem.self), for: indexPath) as! ReviewItem
cell.viewController = self
cell.passwords = reviews[indexPath.section].password
cell.reviewNum = reviews[indexPath.section].reviewNum

so the passwords and reviewNum is changed and print("Password: \(passwords)") and print("Review_number: \(reviewNum)") is successfully work.

So I wrote the code similarly. However, in this case (The first codes that I uploaded),

 let PopUpVC = PopUpVC()
 PopUpVC.pw = self.passwords
 PopUpVC.reviewDoc = self.reviewNum

the value is not changed.

Right below is the parts of the PopUpVC

class PopUpVC: UIViewController{

    var reviewDoc: String = ""
    var pw: String = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        print("PopUpVC - UIViewController called")
        print(pw)
        print(reviewDoc)
    }

 ...

}

I hope someone can tell me what is the problem and how to solve the problem.

Thank you.

CodePudding user response:

You're not initialising your PopUpVC properly. Here's how you should do it:

@IBAction func removeReview(_ sender: Any) {
        
  print("ReviewItem - removeReview function called")
     
  print("Passwords: \(passwords)")
  print("Review_number: \(reviewNum)")
        
  let storyboard = UIStoryboard(name: "PopUp", bundle: nil)
  let alertPopUpVC = storyboard.instantiateViewController(withIdentifier: "VerifyPasswords") as! PopUpVC // make sure this identifier is a PopUpVC class!!!

  alertPopUpVC.pw = self.passwords
  alertPopUpVC.reviewDoc = self.reviewNum

  alertPopUpVC.modalPresentationStyle = .overCurrentContext
  alertPopUpVC.modalTransitionStyle = .crossDissolve

  viewController?.present(alertPopUpVC, animated: true, completion: nil)
        
}

You were initialising your PopUpVC and presenting a different instance initialised via Storyboard.

I hope this helps! :)

  • Related