iOS Unload a View Rather Than Pop - ios

I'd like to know if it is possible to completely unload a view from my navigation controller.
For example say a user gets to the last view controller then taps Done - i want them to be able to restart as if they were launching the app the first time.
I know i can use the pop methods then setup in ViewDidAppear but i'd like the code in ViewDidLoad to be rerun.
Any suggestions?
Thanks.

Move the (necessary) code in viewDidLoad to viewWillAppear:.
You shouldn't necessarily need to do EVERYTHING in viewDidLoad every time the view appears. Anyway, at the end of the day, code that should be executed only the first time the view is loaded should be in viewDidLoad. Code that should be executed every time the view appears should be in viewWillAppear: or viewDidAppear:.

Write your code in viewWillAppear method.Another option is if you have time flexibility then Reload data After Some delay.Suppose,your view load then After 1 second(any time period) call the method which do reload data.Delay method using you can do reload data.

Related

Where to Call REST API in iOS Swift (Standard Approch) [duplicate]

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

What is better place for loading data from API- viewDidLoad, viewWillAppear or viewDidAppear?

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

Where should I load animation in my code(objective-c)?

What is the best place(method) to start animation in view controller in iOS app:
viewDidLoad
viewWillAppear
viewDidAppear
Thanks!
the answer is:
in the viewDidApear method.
the reason:
i want the animation to load after the view appeared and every time it appears if i will do it in the load method it won't happen after i will come back to the ViewController and if i put it in view will apear it may take few milisecs until the view loads so the animation won't start from the first frame so it is the best to put it after the view alredy appeared.
viewDidLoad: would be triggered for the first time when you load the view. In this method your animation won't run everytime you open the view.
viewWillAppear: is before loading the View. viewDidAppear: is after loading the View. These two methods will be called whenever you navigate from other view. If you want the animation to be started before appearing the view, go for viewWillAppear:. Otherwise goto viewDidAppear:. I use viewDidAppear: to make sure my users see the full animation.
viewDidLoad: is generally the place to put view loading or refinement. If you put it in viewWillAppear, you may be needlessly reloading it if the user comes back to this screen and the animation was already loaded.
You may wish to start and stop the animation in viewWillAppear: and viewDidDisappear: if it reduces your memory or processing footprint.

For the first time I click on a cell, it is taking some time to load the next view controller. Why?

I'm initializing the view controller in the didSelectRowAtIndexpath method, as follows,
GuideViewController *gViewController = [[GuideViewController alloc] initWithGuideline:obj withTitle:#"" htmlFilePath:#""];
and then pushing it using the navigation controller as follows,
[[self.parent navigationController] pushViewController:gViewController animated:YES];
This is taking some 3 seconds to load for the very first time I install it in the device, but from the next time onwards its smooth and fast. I'm wondering how its working fast from the next time I select the cell.
Please suggest some ideas to fix this issue.
FYI: The project is Non-ARC.
I have developed an app with a UITabBarController.
And I also wanted my app to be fast. Because some tab were loooooong to load, I've preloaded them by doing :
ViewControllerToPreload.view
Indeed, calling view will call the viewDidLoad method of your UIViewController : ViewControllerToPreload.
You can make this call in the viewDidAppear: method of your current view controller, so it will load your next view controller, which will be faster to appear when called.
I suggest you profile with Instruments and you will find the answer as to what is happening during those 3 seconds.

ViewController won't update after load

I got two viewControllers using a navigation bar. The first viewController displays some data I change on the second viewController.
So if I load the second viewController, a back button appears in the NavBar and I can change my values (and they are stored, I used the debugger). My problem is, after hitting the backButton to come to my firstView Controller, it does not call it's viewDidLoad method. It's clear, that there are no updated values at all, when this function is not called.
At the first start, the viewDidLoad method is called and does what I want it to do. After going back and forth between the viewControllers the method is not called again.
Any solutions?
Thanks!
EDIT:
SOLVED
I did not want to delete my question, maybe someone needs this too:
This method is called every time the view appears, it is probably not defined by default:
-(void)viewDidAppear:(BOOL)animated
{
NSLog(#"View appeared.");
}
But the update code (like [[self view] setNeedsDisplay];) in viewWillAppear.
To make it clear: viewDidLoad is called when your view is loaded. This happens at the first time the view is going to be displayed. When you then navigate to the next view the first view can (depending on your code) still be loaded. Therefore when you navigate back to that view viewDidLoad won't be called because the view is still there.
But every time the view is going to be shown (for example when you navigate back to this view) the method viewWillAppear and viewDidAppear will be called.
Hope that helps.
ViewDidLoad method will get called when there is view controller allocation-initialization happens. In the case of moving back in navigation controller, it is not creating any new view controllers, it is reusing previous references from navigation stack.
Hence viewDidLoad will not get called. As your view controller is already in memory stack. So it will just make the view to reappear on windows and it will call viewWillAppear respectively.

Resources