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.
Related
I am working on a app using UIWebView now getting a issue listed below please help me to sort it out.
In my UIWebView, I show some HTML content from String in it.
However, when I go to another ViewController and return back, it goes white for a second then draws my HTML content again.
Is there any way to prevent this? My html content does not change, so can I set it as fixed content or something to draw it faster ?
This is how I set html in webview:
webView.loadHTMLString(htmlData, baseURL: nil)
Your HTMl code is not changing, so that put your webView load code inside viewDidLoad instead of viewDidAppear, because viewDidAppear always call when your view is appear, where as viewDidLoad called single time when your view is load.
you can execute the code which is directing webview once in a life cycle of app or customize accordingly.
// if you are navigating your application using navigation controller enables you to come back to the rootview without executing whole code of that class associated with view.
// this doesn't apply the whole life cycle of view controller
[self.navigationController pushViewController:vc animated:YES];
// if you navigating through below code this apply the whole life cycle concept of view controller.
[self presentViewController:vc animated:NO completion:nil];
you must have a look about the life cycle of view controller here is a useful apple doc
viewController Life cycle short note
ViewDidLoad - Called when you create the class and load from xib. Great for initial setup and one-time-only work.
ViewWillAppear - Called right before your view appears, good for hiding/showing fields or any operations that you want to happen every time before the view is visible. Because you might be going back and forth between views, this will be called every time your view is about to appear on the screen.
ViewDidAppear - Called after the view appears - great place to start an animations or the loading of external data from an API.
ViewWillDisappear/DidDisappear - Same idea as ViewWillAppear/ViewDidAppear.
ViewDidUnload/ViewDidDispose - In Objective C, this is where you do your clean-up and release of stuff, but this is handled automatically so not much you really need to do here.
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.
I created regular buttons in .xib file and I added a gradient effect to them and shadows in the code in this section:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
... my customized buttons code here
}
When I modally switch to another view controller and then go back to the original one the xib file gets redrawn but all the gradient effects and shadows disappear. Any ideas?
I'm not sure entirely what's going on, but note that after you dismiss the modal view controller, viewDidAppear: gets called again. If you only want to make these buttons once, you could move your custom button code to viewDidLoad.
I haven't experienced this problem before to know what's going on, so it would be helpful to see the code itself. But I suspect that moving the code to viewDidLoad would solve the problem.
When modal View is dismissed, ViewDidLoad is not called, But ViewWillAppear and ViewDidAppear is called, But you say that the code is written in ViewDidAppear, and still it doesnt work. I suggest you to write that code in ViewWillAppear and check.
What is the difference between viewDidLoad and viewDidAppear? What kind of initialization or custom code goes into those functions?
e.g. presentModalViewController works only when present in viewDidAppear and not on viewDidLoad.
viewDidLoad is called exactly once, when the view controller is first loaded into memory. This is where you want to instantiate any instance variables and build any views that live for the entire lifecycle of this view controller. However, the view is usually not yet visible at this point.
viewDidAppear is called when the view is actually visible, and can be called multiple times during the lifecycle of a View Controller (for instance, when a Modal View Controller is dismissed and the view becomes visible again). This is where you want to perform any layout actions or do any drawing in the UI - for example, presenting a modal view controller. However, anything you do here should be repeatable. It's best not to retain things here, or else you'll get memory leaks if you don't release them when the view disappears.
See: https://developer.apple.com/documentation/uikit/uiviewcontroller
Simply put, you would want to create any controls or arrays in viewDidLoad, where as in viewDidAppear is where you would want to refresh those controls or arrays.
viewDidLoad is called once when the controller is created and viewDidAppear is called each time the view, well, DID appear. So say you have a modal view that you present, when that view is dismissed, viewDidAppear will be called, and viewDidLoad will not be called.
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.