I use this line of code in my iOS app
self.navigationController?.popToRootViewControllerAnimated(false)
It takes the user back the login page, but it does not reload the login with all the most recent data.
It will not run this code again:
override func viewDidLoad() {
super.viewDidLoad()
...
}
How do I make the segue cause the viewDidLoad again?
Use viewWillAppear method for that.
You are sending a web call from viewDidLoad(). So to update your viewController add your web request method in viewWillAppear().
Note: viewDidLoad gets called only when the view controller loads which happen once. However viewWillAppear gets called before it appears which will often happen if you're hopping back and forth between views.
Related
I am making an IOS app where i am calling an API in viewDidLoad method of view controller. Now i want to reload the same view controller with the data that comes from server. How many ways are there to do this task and what would be the best way?? Please help me.
Thanks!!
viewDidLoad method is called first time when UIViewController is first loaded and when it pop and then you reenter in it at that time viewDidLoad is called. So if you want to load the API only once then viewDidLoad is the best place to call an API.
viewWillAppear called every time when you enter in that UIViewController and it is the place load the API when you want to get refreshed data (updated data).
viewDidAppear also called like viewWillAppear but bit late called than viewWillAppear so if you want to call the API every time than the best place is viewWillAppear method.
Because viewDidAppear method called late from viewWillAppear method and you are just requesting the API so the response of API may be late and If your UI change based on API response then it will stuck the application UI so there is a best place to call API either viewDidLoad & viewWillAppear methods.
viewDidLoad is called once. If you use navigation controller and do back and forth ou view controller this viewDidLoad method will never be called. Until you create this ViewController again (i.e [navContoller pushViewController]). If your api data will never changed the life cycle of this View Controller the this is the better place to call your API. But if your api data need to call frequently [i.e. back and push.forth this view controller] then you should not call api here.
viewWillAppear: before a view controller shows.If you call you api
inside this method you UI will stack until the data loading finish. which looks odd.before load the view of viewController this "viewWillAppear" method is called. This is the reason, it's name is "viewWillAppear". That means this view will load some time later (i.e some micro second later). If you call your api here after what will happen lets analyze. Say, your api return response after 10 sec. Then UI will freeze/stuck for 10 sec and you will see after this 10 sec later your view will called.
viewDidAppear: after finished a view controller showing.So, you need to called your loading API inside this method.
viewDidAppear is definitely not the one you want to use, it will 'pause' the view from responding while you're loading the data.
Normally viewDidLoad is the one you want to place it.
If we stay in the same ViewController,the three method viewdidload,viewwillappear,viewdidappear will not called again.So we stay in the same view controller, we get the data from the server,we should call the reload method after get the data.
Hope this answer can help you.
I think viewWillAppear is best place for loading data from API. Because viewDidLoad called once when the view is being loaded, but viewWillAppear will called when it loaded from its parent view or child view.
You don't need to call API every time you navigate to view controller, you need to call it one time.
If you have a TableView with Cell and this cell get from API and will open new ViewController when you press on it.
So here you will not add your API in :
viewWillAppear()
viewDidAppear()
You will add it one time in viewDidLoad() while we need to minimize num of requests as many as we can.
Example like this: navigation controller:
suppose Fruits and Cars will present from API.
when you click on the fruits cell you will navigate to below viewController:
So when you want to go back to the first view controller, clearly you dont need to reload api while it is already exist.
In this case we use viewDidLoad() to handle API request
i want to run this code before the initial view controller even opens the view so the app knows where to redirect the user if the user is logged in. how can i do that? the code is here:
if let firstCheck = NSUserDefaults.standardUserDefaults().objectForKey("username"){
self.performSegueWithIdentifier("View1ToView2Segue", sender: nil)
}
i put the code after override func viewDidLoad() but it didnt work.
Unfortunately, you cannot do that. :( In this case, I would have a load image or something similar and have it fade away if the segue is not supposed to be performed. You would run this in the viewDidAppear method.
I am making an IOS app where i am calling an API in viewDidLoad method of view controller. Now i want to reload the same view controller with the data that comes from server. How many ways are there to do this task and what would be the best way?? Please help me.
Thanks!!
viewDidLoad method is called first time when UIViewController is first loaded and when it pop and then you reenter in it at that time viewDidLoad is called. So if you want to load the API only once then viewDidLoad is the best place to call an API.
viewWillAppear called every time when you enter in that UIViewController and it is the place load the API when you want to get refreshed data (updated data).
viewDidAppear also called like viewWillAppear but bit late called than viewWillAppear so if you want to call the API every time than the best place is viewWillAppear method.
Because viewDidAppear method called late from viewWillAppear method and you are just requesting the API so the response of API may be late and If your UI change based on API response then it will stuck the application UI so there is a best place to call API either viewDidLoad & viewWillAppear methods.
viewDidLoad is called once. If you use navigation controller and do back and forth ou view controller this viewDidLoad method will never be called. Until you create this ViewController again (i.e [navContoller pushViewController]). If your api data will never changed the life cycle of this View Controller the this is the better place to call your API. But if your api data need to call frequently [i.e. back and push.forth this view controller] then you should not call api here.
viewWillAppear: before a view controller shows.If you call you api
inside this method you UI will stack until the data loading finish. which looks odd.before load the view of viewController this "viewWillAppear" method is called. This is the reason, it's name is "viewWillAppear". That means this view will load some time later (i.e some micro second later). If you call your api here after what will happen lets analyze. Say, your api return response after 10 sec. Then UI will freeze/stuck for 10 sec and you will see after this 10 sec later your view will called.
viewDidAppear: after finished a view controller showing.So, you need to called your loading API inside this method.
viewDidAppear is definitely not the one you want to use, it will 'pause' the view from responding while you're loading the data.
Normally viewDidLoad is the one you want to place it.
If we stay in the same ViewController,the three method viewdidload,viewwillappear,viewdidappear will not called again.So we stay in the same view controller, we get the data from the server,we should call the reload method after get the data.
Hope this answer can help you.
I think viewWillAppear is best place for loading data from API. Because viewDidLoad called once when the view is being loaded, but viewWillAppear will called when it loaded from its parent view or child view.
You don't need to call API every time you navigate to view controller, you need to call it one time.
If you have a TableView with Cell and this cell get from API and will open new ViewController when you press on it.
So here you will not add your API in :
viewWillAppear()
viewDidAppear()
You will add it one time in viewDidLoad() while we need to minimize num of requests as many as we can.
Example like this: navigation controller:
suppose Fruits and Cars will present from API.
when you click on the fruits cell you will navigate to below viewController:
So when you want to go back to the first view controller, clearly you dont need to reload api while it is already exist.
In this case we use viewDidLoad() to handle API request
I have a modally presented View Controller called ChangeViewController that allows the user to change some information regarding his/her profile. When the user is done, the 'ACCEPT CHANGES' button is pressed and I use an unwind segue to exit out of the ChangeViewController and go back to the main screen.
Is it possible for me to refresh the data of the entire app before performing this unwind segue? As of now, the information on the home screen and other screens remains unchanged after user modifications. Only after an app reboot does the information update. Is there a way to programmatically 'reboot' so all View Controllers are up to date? More specifically, is there a way to call the viewDidLoad functions of all View Controllers so their data is updated?
Solutions in Swift preferred. Thank you!
I oftentimes updateUI after a user changes with a function like this. This is pretty simple, and you just call the function whenever your UI is updated.
func updateUI() {
// Redraw your labels, update your UIElements, do what you have to do
}
A way you can call this function from a modally presented ViewController without closing the app is with delegation, since modal presentation does not throw the old ViewController out of the stack and heap, Delegation works like this:
In your modal controller:
protocol ChangeViewControllerDelegat: class {
func updateUI(sender:UIButton)
}
class ChangeViewController: UIViewController {
weak var delegate: ChangeViewControllerDelegat?
func opChangingUserSettings() {
// Change settings with your code
// tell your ViewController to do it.
delegate?.updateUI()
}
in your mainVC
class MainViewController: UIViewController, ExtensionViewControllerDelegate {
func updateUI() {
// Redraw your labels, update your UIElements, do what you have to do
}
}
Hope that helps!
By the way, are you trying to change language by any chance on the fly? If so, I can show you how to do that. If not, and If I understand your question, this should work.
Well, you can use the local notification for your scenario.
As in, when the user is done with the changes and presses the button to accept the changes, there you can post a local notification to reload the data.
And you can listen to that notification in all of your viewControllers and reload the data there, like if it is a table view then you can simply call the reloadData method on tableview to achieve it.
I have two view controller A & B.On ViewController A i am displaying data from server.I have nav button Update to go to ViewController B.On B i can update the data which was shown on ViewController B.Now when data is updated on server i show an alert 'Update Success' on click of Ok i go to previous ViewController.But i don't see the updated data because i removed the ViewController B from stack but A is not reloded.
Code for going back to ViewController A
[self.navigationController popViewControllerAnimated:YES];
I assume you are sending a web call from viewDidLoad(). So in order to update your viewController A Add your web call method in viewWillAppear()
The state of your "data" on view controller A has not changed and you need to refresh it. If you're fetching data for view controller A within -viewDidLoad, try moving it to a different life cycle method like -viewWillAppear.
Remember, -viewDidLoad gets called only when the view controller loads which happens once, however -viewWillAppear gets called before it appears which will happen often if you're hopping back and forth between views.
You have to call your API method in viewWillAppear(). So, whenever you come back from any viewController, your updated server data will reload itself.