When does the completion block of dismiss view get executed? Is it after or before the user sees the view dismissed?
I have this code to make a toast with a message inside completion block but never see the toast after this view dismissed.
self.dismiss(animated: true, completion: {
self.view.makeToast(message: "Employee has been assigned successfully.", duration: 2.0, position: HRToastPositionCenter as AnyObject, title: "Succeeded!")
})
What I want is the user can see the toast when the view gets completely dismissed?
How do I do this ?
The block to execute after the operation's main task is completed.
The first option is to tell the view to dismiss itself using its presentation mode environment key. Any view can read its presentation mode using @Environment(\. presentationMode) , and calling wrappedValue. dismiss() on that will cause the view to be dismissed.
When it comes time to dismiss a presented view controller, the preferred approach is to let the presenting view controller dismiss it. In other words, whenever possible, the same view controller that presented the view controller should also take responsibility for dismissing it.
The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.
You can delegate the event from presented controller to the parent and handle it there.
In EmployeePickerViewController (or whatever your modal controller is called):
@protocol EmployeePickerDelegate {
func employeeAssigned()
}
class EmployeePickerViewController {
weak delegate: EmployeePickerDelegate!
}
When employee assignment is finished just call delegate's method:
delegate?.employeeAssigned()
In MainViewController when you present modally:
employeePicker.delegate = self
present(employeePicker, animated: true, completion: nil)
In MainViewController below:
extension MainViewController: EmployeePickerDelegate {
func employeeAssigned {
dismiss(animated: true, completion: {
self.view.makeToast(message: "Employee has been assigned successfully.", duration: 2.0, position: HRToastPositionCenter as AnyObject, title: "Succeeded!")
})
}
}
For UIViewController.dismiss(animated:completion:)
The completion handler is called after the viewDidDisappear(_:) method is called on the presented view controller.
Source
For UIViewController.present(_:animated:completion:)
The completion handler is called after the viewDidAppear(_:) method is called on the presented view controller.
Source.
If you don't know when that is, this is the order of the UIViewController load, appear and dissapear methods
normally we initialize data objects and controls. It will create all the necessary memory for all controls/data objects for this view. i.e. In the above case, anotherView and btnView, they will keep the same memory addresses for the whole life cycle.
Called before the view is added to the windows’ view hierarchy. So it is ideal for updating the viewcontroller’s data.
Called after the view is added to the windows’ view hierarchy.
Called before the view is removed from the windows’ view hierarchy.
Called after the view is removed from the windows’ view hierarchy.
Source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With