UIWebView redraws on view did appear, how can I prevent this? - ios

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.

Related

Is a view "reloaded" when you have left it?

I have a view controller. It has some data and values in it. Then it presents modally to another view and moves around and some stuff happens. Then at some point, you self.dismissViewController(). Once back at the original view, can I count on the original data being there? Any help is greatly appreciated. Thanks in advance!
They will not be affected unless you specifically write code to do that for you.
For example, if you call a network request in the viewDidLoad() and add the data to some views, labels, etc. Then you leave that ViewController and come back, nothing will change, i.e., the network request will not be called again.
If you do want to change values in the ViewController every time it appears, use the viewDidAppear() delegate method.
for reloading view on dismiss you must use viewWillAppear() because this working every time before rendering view
calling dismissViewController will remove the viewController. If you do not have a strong reference to the view controller stored elsewhere, dismissing it releases the memory associated with it.
If the presented view controller must return data to the presenting view controller, use the delegation design pattern to facilitate the transfer.
So if the data was not modified in the presented controller then it will be the same then you can depend on it
reference: Apple docs

Delete the only one page in UIPageViewController

Whenever I want to refresh my UIPageViewController I call the following snippet, including when I want to delete a viewController from my UIPageViewController.
[self setViewControllers:#[currentViewController] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
Everything is working just fine... except when I want to delete the one and only viewController from my UIPageViewController because I cannot pass an empty array as the first parameter. In fact I get an exception if I do so.
How can I force a refresh and not set the current viewController for my UIPageViewController?
Right now I am passing a dummy ViewController but I don't find that very pretty. If I do so I get a page indicator even though I am returning zero in my (NSInteger) presentationCountForPageViewController:(UIPageViewController*)pageViewController overload.
The question makes no sense. There is no such thing as a page view controller without a view controller. If your goal is to hide the page view controller's view, hide it (or even rip the whole thing right out of the interface).

Code in viewDidLoad runs every time it is called

Hi all I am doing a course in Udemy, and the code calls for placing code in the viewDidLoad function as shown below:
override func viewDidLoad() {
super.viewDidLoad()
placesArray.append(["name":"Taj Mahal", "lat":"27.175607", "lon":"78.042112"])
}
The array append should only run once, however, when I segue to another viewController and come back, it runs the code to append again. So I now have an array with 2 rows, both of which are Taj Mahal.
I thought that the viewDidLoad function only runs code once?
Is there a way around this?
Thanks.
Addendum:
I am using Swift, so I don't see any alloc and init while creating and launching the viewController. And weird as it sounds, the video tutorial has it working in the viewDidLoad and the trainer is using the storyboard to segue from the initial table view controller to a map view on a view controller and just has a back button on the map view that segue's back to the table view controller via the storyboard as well. - Could be because I have the latest version of the Swift language and the trainer was using an earlier version, (cause I noticed some slight differences in coding earlier) but you never know. Either way whenever he touches the back button it does not run the append code anymore.
I am trying to get in contact with the trainer as some of the suggestions here, though they are good don't seem to work.
I will put the solution in here once I get in contact with the trainer.
The viewDidLoad method is called when your view controller's view finishes loading. The view will load when a view controller's view property is nil and something attempts to access it.
UIViewController *myVC = [[UIViewController alloc] init];
UIView *aView = myVC.view; // this loads myVC's view; viewDidLoad is called when it completes loading.
If the view has unloaded (usually due to memory limitations), it will be called when the view property is next accessed.
Manipulation of data sets should generally not be done within view methods. Consider moving this to the init of the view controller (or to a different "datasource" class).
I suppose you are trying to do data initialisation in viewDidLoad. If there is no other operation on placesArray before viewDidLoad, then instead of append, what about setting the placesArray directly:
placesArray = ["name":"Taj Mahal", "lat":"27.175607", "lon":"78.042112"]
Then even if your view is unloaded for some reasons. Taj Mahal will still be added once only.
viewDidLoad is called whenever the view controller's view property is set. When does this happen? It depends on how the view controller is contained:
UINavigationController
- View Controller views are loaded as they are added to the navigation stack and "unloaded" (although the viewDidUnload method is deprecated) as they are removed.
UITabBarController
- View Controller views are loaded as they are added to the tab bar regardless of whether they are on screen or not. They stay loaded as you change from tab to tab.
Depending on your needs and use case, you can create your own view controller container that does what you need. Checkout the Apple docs on the proper way to do this:
https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html

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.

How do I call viewDidLoad Manually?

I'm having a slight problem with iOS. I am passing data back and forth between two view controllers using protocols and manually switching views. My problem is that when I dismiss the top view, viewDidLoad for the bottom view isn't called again. Since I'm sending information from my second view to my first view, I need viewDidLoad to be called so I can handle the information I'm passing. If you have any ideas on how to do this, any help is appreciated. Thank you.
- (void)viewDidLoad {
[super 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.
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
}
Is called when the view is actually visible, and can be called multiple times during the lifecycle of a View Controller (example when a Modal View Controller is dismissed and the view becomes visible again)
use -(void)viewDidAppear:(BOOL)animated instead of viewDidLoad
I am passing data back and forth between two view controllers using protocols and manually switching views. My problem is that when I dismiss the top view, viewDidLoad for the bottom view isn't called again. Since I'm sending information from my second view to my first view, I need viewDidLoad to be called
No you don't, and the fact that you think you do makes me think something else may be going wrong here.
Consider this: if you are truly passing data back from the second view controller from the first, then the first view controller has the data and can update itself now. It exists, and it has a view. So it should update itself on the spot, as you hand it the data. Then, when you dismiss the second view controller, the first view controller's view will appear - already updated. In effect, dismissing the second view controller merely reveals the first view controller's view, which was there all along, and was updated even though it wasn't showing.

Resources