The app starts on ViewContoller
I push TutoViewContoller
On some action, I pop TutoViewContoller
I would like to call a method of ViewContoller just after popping TutoViewController. How can I do that ?
You can implement viewWillAppear of ViewController. If you really need to distinguish between TutoViewController and other viewcontroller you have many options to call before you pop it: delegate pattern, passing reference of ViewController to TutoViewController etc.
Related
Throughout my app I use a navigation controller to push and pop my view controllers. When I pop from one ViewController, I check to see if I can reload the data in the previous one with this:
_ = self.navigationController?.popViewController(animated: true)
if let previousViewController = self.navigationController?.viewControllers.last as? AnimalsVC {
previousViewController.tableView.reloadData()
}
This works every time, but now I need to do the same with another view, but it's not apart of the navigation controller because I modally present (instead of pushing it to the navigation controller). Now there's no way I can access the previous ViewController like before and I can not figure out how to access the previous ViewController.
How can I access the previous ViewController to reload the tableview like the example code without accessing the navigation controller?
I know this is possible with notifications, but I prefer not to use that method if possible!
First of all, It's not necessary to access the previous ViewController to reload tableview(or any other func)
I recommend you to use Delegate to achieve the same feature.
Holding a reference to the previous viewController in the way you mentioned will make your app very hard to maintain when your app gets more complicated.
You can call tableview.reloadData() in viewWillAppear method in the controller that you present modally
I know, I can programmtically do segue action by using:
-performSegueWithIdentifier:sender:
I also know the other segue related method on UIViewController, -prepareForSegue:sender:, in which I can do customization on the destination view controller.
But I am wondering, which one is called first?
say in a scenario, there is ViewControllerOne that has a button which triggers the -performSegueWithIdentifier:sender: , then goes to ViewControllerTwo.
The first is performSegueWithIdentifier:sender: and actually this is the one you performed.
The second call you mentioned prepareForSegue:sender: is a delegate call, which you are not forced to implement
Got it myself:
Before performSegueWithIdentifier
Before prepareForSegue
After prepareForSegue.
After performSegueWithIdentifier
What function gets triggered when a user hits the back button on a push page? I need to set some properties before going back to the previous page. ViewWillDisapper is not what I'm looking for
You will want to use either viewWillDisappear or viewDidDisappear to monitor a user going back on a pushed page (assuming you mean this is inside a navigation controller).
Just like when you call your instance method of UINavigationController to "pushViewController:(BOOL)animated:" guess what method is called to POP a view controller? try something like
[self.navigationBar.backButton //Not sure if this is what its called addTarget:self selector:#selector(popViewController)]
and fill in the rest of the method with UIEventTouchUpInside so when the user presses the back button on navigation bar it will jump into you're overrided function.
Make your View inherit UINavigationController protocols and then overide popViewController method.
I suggest looking at UINavigationController class reference if that didn't answer your question.
I'm sure this will be a simple answer, maybe a method I'm missing to implement. Here goes:
In my Controller1 I use a pushViewController to push a new view Controller2 onto my view stack. I'm getting a back button.
Now, when Back button is pressed in my Controller1 I want to be able to detect that Controller2 is being popped and we're back in Controller1. Makes sense?
I was trying to do that with ViewDidAppear, but I'm not sure how to detect a popped controller. There are other answers here but they all show examples in Controller2 on viewWillDisappear
How would I do that in Swift?
Without knowing more you should pass a reference of the first controller to the second controller so that the second controller can tell the first when it is done (this is a simple delegate relationship, it could also be implemented using a block / closure).
Ideally the first controller should be responsible for dismissing the second controller, either when it gets this callback or, more appropriately, directly when the 'dismiss' button is tapped.
I'm working on an iPhone app where I move through push through several view controllers. On the last on I [self.navigationController popToRootViewControllerAnimated:YES]
I want to ask is there a way to detect that I just came from ViewController7 when i return to the ViewController1?
The reason being i'd like the viewDidAppear to behave in a certain way if it is.
Otherwise is it possible to rerun the ViewDidLoad? (I'm presuming its not).
Thanks.
You could have your viewController1 conform to the UINavigationControllerDelegate protocol and become the UINavigationController's delegate. Then in navigationController:willShowViewController:animated: check if the controller to be shown is viewController1, check your UINavigationController's visibleViewController and set some variable in viewController1. Then in viewDidAppear you can animate appropriately.
I'd use the delegation design pattern to set a protocol method to send information back regarding what view controller you are in.