Dismiss ViewController And reload previous / skipped data back?

My application downloads updates from the server. To do this, he compares the data on the device with what is available on the server. If there is a mismatch, the logical "updateAvailable" is set to true, and an interactive UILabel is displayed to the user indicating that he can download the update.

(Simplified code)

class ViewController: UIViewController { @IBOutlet weak var updateLabel: UILabel! func updateChecker(){ let deviceVersion = self.settingsController.getDeviceVersion() //Get Server Version (Async) _ = settingsController.getServerVersion() { (data) -> () in dispatch_async(dispatch_get_main_queue()){ if deviceVersion != data{ self.updateAvailable = true } else{ self.updateAvailable = false } //Update UI self.updateUI() } } } func updateUI(){ if updateAvailable{ updateLabel.text = "Update Available" } else{ updateLabel.text = "App Up To Date" } } 


When you touch this update label, the application launches the user transition to "updateViewController":

  @IBAction func getUpdateLabelPressed(sender: UITapGestureRecognizer) { if updateAvailable{ self.performSegueWithIdentifier("segueToUpdateView", sender: self) } 



UpdateViewController downloads data from the server (async) and then rejects:

 class UpdateViewController: UIViewController{ override func viewDidLoad() { super.viewDidLoad() //Create Settings Controller + Get Update let sc = SettingsController() sc.getUpdate({ (success) -> () in if success{ dispatch_async(dispatch_get_main_queue()){ print("Update Success") self.dismissViewControllerAnimated(true, completion: nil) } } else{ dispatch_async(dispatch_get_main_queue()){ print("Update Failed") self.dismissViewControllerAnimated(true, completion: nil) } } }) } 


When the UpdateViewController is fired, the main ViewController is displayed, however, 'updateAvailable' is still set to true, and updateLabel is still active.

Running another custom Segue back in the ViewController / Root ViewController is not an option for me, as it interrupts the background update selection, creating more than one instance of the "ViewController" on the stack.

onViewDidAppear () is also unacceptable in this case, as this can lead to heavy network / server traffic.

How can I reject the UpdateViewController AND Reload the previous ViewController ViewController OR Send data that runs the ViewController updateChecker () method? OR clear the entire ViewControllerStack and restart the application?

I understand that a delegate arrangement seems appropriate, but previous SO questions and answers on this topic, such as this> for Objective-C, and I was not able to adapt these answers for Swift 2.0.

Any help would be greatly appreciated.

+4
source share
2 answers

Create the clearUpdateAvailable close clearUpdateAvailable in UpdateViewController:

 class UpdateViewController: UIViewController { var clearUpdateAvailable: (()->())? override func viewWillDisappear(animated: Bool) { super.viewWillDisappear(animated) clearUpdateAvailable?() } } 

You can call clearUpdateAvailable in viewWillDisappear or in the method in which you receive data from the server.

In ViewController, put a close when presenting UpdateViewController by ID.

 let storyboard = UIStoryboard(name: "Main", bundle: nil) if let updateViewController = storyboard.instantiateViewControllerWithIdentifier("updateViewController") as? UpdateViewController { updateViewController.clearUpdateAvailable = { [weak self] in self?.updateAvailable = false self?.updateUI() } presentViewController(updateViewController, animated: true, completion: nil) } 
+1
source

Pass the main ViewController to the UpdateViewController in the prepareForSegue method.

When you are done updating, update the updateAvailable flag for the ViewController .

Thus, when the main ViewController rejected and appears, the updateAvailable in the ViewController will be YES .

Remember to set accessibilty for the updateAvailable checkbox and the ViewController link in the UpdateViewController to the public. to be able to update these values ​​away from the class.

+1
source

All Articles