简体   繁体   中英

View controller's data changes on 2nd attempt

Hey my code changes data in another view controller on 2nd attempt on 1st just showing default values

Code inside button

@IBAction func check(_ sender: Any) {
    makeRequest()
    let fin = UIStoryboard(name: "FinalViewController", bundle: nil)
    let pop = fin.instantiateInitialViewController()! as! FinalViewController
    pop.img = icon
    pop.state = state
    pop.fail = failed
    self.present(pop, animated: true)   
}

Code inside 2nd view controller

class FinalViewController: UIViewController {
    @IBOutlet weak var weathure_icon: UIImageView!
    @IBOutlet weak var status: UILabel!
    var fail = false
    var img = ""
    var state = ""
    override func viewDidLoad() {
        if fail == false{
            super.viewDidLoad()
            status.text = state
            weathure_icon.image = UIImage(named: img+".png")
    }
}

Please check three things in your code inside the button action.

  1. Please cross-check, is makeRequest() asynchronous request??

  2. is icon, state and failed parameters request coming from makeRequest() method, in this case you need to handle request data on main thread.

  3. Please replace following in your code:

This

let fin = UIStoryboard(name: "FinalViewController", bundle: nil) let pop = fin.instantiateInitialViewController()! as! FinalViewController

With

let fin = UIStoryboard(name: "STORYBOARD_NAME", bundle: nil) let pop = fin.instantiateInitialViewController(identifier: "FinalViewController")! as! FinalViewController

Hope this will help you and make success with your implementation :)

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