I am using UICollectionView inside a UIPageViewController.
Each page is an instance of the same subclass of UICollectionViewController.
Whenever I do a page swipe to see a page I haven't seen before, the animation gets a small delay (few milliseconds) to start because of the time UICollectionView takes to go through all the delegate/datasource methods.
So I was trying to implement a pre-caching system to load the viewcontroller before the user actually starts swiping.
I am able to create it easily, but, for some reason, whenever I call reloadData on the non-visible ViewController, none of the UICollectionViewDelegateFlowLayout delegate methods get called.
So far I have tried to use: setNeedsDisplay, setNeedsLayout, setLayoutIfNeeded and invalidateLayout to force them to get called, but nothing is working.
The viewcontroller is not nil. The view is not nil.
Does anyone know what could be the reason? Is it because the viewcontroller is not in the viewport?
Loading the view controller first is probably not the best idea.
You should rather cache the data source.
About reloadData not working properly with a non displayed controller, you are right, it's because the controller is not present.
The delegates won't fire unless the view is visible.To counter the delay you are facing you would have to ditch the collectionview and use collectionview controller and add those collectionview controllers as child to page controller
Note: Set the frame appropriately for each collection controller
CollectionController *collectionController = [self.storyboard instantiateViewControllerWithIdentifier:#"Calculate"];
[self addChildViewController:collectionController];
collectionController.view.frame = self.calculationView.bounds;
[self.pageControllerView addSubview:collectionController.view];
[collectionController didMoveToParentViewController:self];
Related
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
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.
When is the best time to call [reloadData] on a UITableView? viewDidAppear or viewWillAppear? If a view is unloaded, will the underlying tableview be unloaded as well?
The reason I am asking is due to some behavior I am seeing. Let's say a view is asked to reload its datasource, but is deallocated before it can finish. A scenario would be with a UITabBarController and navigating to a different view than the ViewController being selected. So what happens is that viewWillAppear gets called, but viewDidAppear does not (since I navigated away).
As a result, the ViewController gets deallocated (along with its model data), but if I am calling reloadData in viewWillAppear, the "cellForRow", and "numberOfRows" methods are invoked which causes a "deallocated instance" error. Does that make sense? Do you normally put in code that checks for nil if the model can be changed anytime the view is requested to appear?
I would load the table once the view has loaded, it allows for all of the UIObjects to be created before messing with them. Not sure, but I believe the table is automatically unloaded with the superview.
Take a look at this question>>>
Objective C - Correct way to empty and reload UITableViewController with NSMutableArray
before you get the datasource from server or anywhere, you can display the tableview with a blankcell(a cell that you can draw sth like 404 web page) , when you get the data, then reload the tableview.
remember to set number of cell to 1 for the blankcell
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.
I have an app that has four tabs. I have four UINavigationControllers, each of which is bound to the UITabBarController by setting the TabBarItem property of each controller and then adding the controllers to the UITabBarController. That's all standard iOS fare.
One of these tabs loads up a UITableView that displays some data from a remote web service. Because the web service calls routinely take about 1.5 seconds to complete and deserialize, I'd like to display a "loading" UIView with the text "Loading..." when the corresponding tab is selected. I've tried this a couple of ways:
assigning a delegate to the ViewControllerSelected event on the UITabBarController, and checking to make sure that the controller selected is the one I'm interested in. Then I try to show the "loading" UIView. This all happens in AppDelegate;
overriding the ViewWillAppear and ViewWillDidAppear methods on the controller itself. Show the LoadingHUDView in the ViewWillAppear method and hide in the ViewDidAppear method.
In both cases:
The "loading" UIView is a local variable (lhv) and is already setup with the text I want.
lhv.SharedApplication.KeyWindow.Add(lhv);
lhv.StartAnimating(); (the UIView contains a UIActivityIndicatorView)
Perform the service calls and refresh the UITableView
lhv.StopAnimating();
lhv.RemoveFromSuperview();
(And yes, I am aware that I don't need to use lhv.SharedApplication.KeyWindow in AppDelegate because I have the local "window" member available to me.)
The problem is that the "loading" UIView doesn't render until AFTER the UINavigationController has rendered its view. I want the LoadingHUDView to appear IMMEDIATELY, as soon as the UITabBarController is tapped. I've tried using InvokeOnMainThread() as well, but no luck there.
Any ideas?
>.< Figured it out. Just had to spin up the loading UIView in a new thread at the beginning of ViewWillAppear or ViewDidAppear. Then just have to do the cleanup at the end of the respective method by removing the view from the superview. Should've thought of that one a long time ago! D'oh!
declare spinner separatly; and add
MyView.addsubview(_spinner);