Home > Blockchain >  Whay view controller2 data couldn't move to view controller1 property in Xcode
Whay view controller2 data couldn't move to view controller1 property in Xcode

Time:11-08

First view controller 1 code

import UIKit

class ViewController: UIViewController {

    @IBOutlet var resultEmail: UILabel!
    @IBOutlet var resultUpdate: UILabel!
    @IBOutlet var resultInterval: UILabel!
    

    var paramEmail: String?
    var paramUpdate: Bool?
    var paramInterval: Double?
    

    override func viewWillAppear(_ animated: Bool) {
        

        if let email = paramEmail {
            resultEmail.text = email
        }
        

        if let update = paramUpdate {
            resultUpdate.text = update==true ? "Auto":"NoAuto"
            

        }
        

        if let interval = paramInterval {
            resultInterval.text = "Every \(Int(interval))min"
        }
    }
}

Second view controller

import UIKit
class FormViewController: UIViewController {
    

    @IBOutlet var email: UITextField!
    @IBOutlet var isUpdate: UISwitch!
    @IBOutlet var interval: UIStepper!
    

    @IBOutlet var isUpdateText: UILabel!
    @IBOutlet var intervalText: UILabel!
    

    

    @IBAction func onSwitch(_ sender: UISwitch) {
        if (sender.isOn == true) {
            self.isUpdateText.text = "Refresh"
        } else {
            self.isUpdateText.text = "NoRefresh"
        }
    }
    

    @IBAction func onStepper(_ sender: UIStepper) {
        let value = Int(sender.value)
        self.intervalText.text = "Every \(value)min"
    }
    

    

    @IBAction func onSubmit(_ sender: Any) {
        let preVC = self.presentingViewController
        guard let vc = preVC as? ViewController else {
            return
        }
        

        vc.paramEmail = self.email.text
        vc.paramUpdate = self.isUpdate.isOn
        vc.paramInterval = self.interval.value
        

        self.presentingViewController?.dismiss(animated: true)
    }
    

}

No error is found on debugging. Only vc.paramEmail of second view controller(also, vc.paramUpdate and vc.Interval data) couldn't move to resultEmail.text of first view controller. So, Email label of first view controller don't show any data of second view controller on emulator.

CodePudding user response:

The information you provided is not enough to locate the problem.So I will give you some suggestions and you can see if they can help you.

First, when you try to override the viewWillAppear(_:) method of UIViewController, you must call super.viewWillAppear(_:) at some point in your implementation. Have a look at https://developer.apple.com/documentation/uikit/uiviewcontroller/1621510-viewwillappear

Second, viewWillAppear(_:) is not always a reliable place to reload UI. Depending on the style you use when presenting a view controller, the method may not be called during dismissal.

Try this:

// First view controller 1 
class ViewController: UIViewController {

    @IBOutlet var resultEmail: UILabel!
    @IBOutlet var resultUpdate: UILabel!
    @IBOutlet var resultInterval: UILabel!
    
    var paramEmail: String? {
        didSet {
            resultEmail.text = paramEmail
        }
    }

    var paramUpdate: Bool? {
      didSet {
        resultUpdate.text = paramUpdate
      }
    }

    var paramInterval: Double? {
      didSet {
        resultInterval.text = paramInterval
      }
    }
}

CodePudding user response:

You can pass data between view controllers in Swift in 6 ways:

  1. By using an instance property (A → B)
  2. By using segues with Storyboards
  3. By using instance properties and functions (A ← B)
  4. By using the delegation pattern
  5. By using a closure or completion handler
  6. By using NotificationCenter and the Observer pattern

you can check this link

I think you are passing data from B -> A so you can use the delegate approach.

  • Related