ViewDidLoad when returning to higher level UITableView - ios

I've built a large tableview and have found that if I go down a step then click 'back' that the viewdidload function is not called. I need to run a function every time this layer of the UITableView is viewed. Is there a function that will run regardless of whether the view has been previously loaded?

viewDidLoad is called once and only once when the view gets loaded. In your situitation, viewWillAppear might not work as well because it's only called only if the view gets loaded on the canvas. You might need to pass a delegate to the table view controller or view.

You can use :
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}

Related

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

TabBar Item, every viewDidLoad called when app launch

The iPad app, I have a TabBarViewController which contain five VC:A,B,C,D,E.
When app launch, every ViewDidLoad called, after that I click the tabBar item, the viewDidLoad won't called again.
I don't know why?
Thanks for help!
If you mean that every viewDidLoad is being called on its own upon application launch, then there must be something you're doing to trigger these events. If however, you mean that each viewDidLoad is only being called the first time you select each tab, then yes this is the intended behavior. Loading each view controller into memory as it's needed.
If you're interested in being notified every time a view controller was selected and therefore about to appear on screen, add the logic to viewWillAppear: instead of viewDidLoad.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// do stuff
}

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.

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.

Event for first load of a view controller in a UINavigationController?

When my view controller is first presented, I want it to potentially update a cache that provides the data for that view. However, when the user taps the back button from a deeper view controller to return to this view controller, I don't want to update the cache again.
Which event should I be using?
in init, I don't have all the parameters I need yet.
viewWillAppear will be fired every time the view will appear.
viewDidLoad will be fired every time the view has been loaded from the nib, which I believe could happen a second time if there's a memory warning. (Or is this wrong?) Since this is not a memory resident cache, it seems the wrong place to handle this.
having the caller call something extra is inelegant, if there's a built-in way to handle this.
To clarify, this is not a memory resident cache. This is parsing an XML file to binary. The binary is loaded and unloaded in viewDidLoad and viewDidUnload. This is a prerequisite for that step, making sure the binary is up-to-date prior to it being loaded.
Using init may work, but I would recommend a simple subclass of UINavigationController. Create a new method called setRootTableViewController:(UITableViewController *)controller, or something like it. In the method implementation call this:
[controller.tableView reloadData];
[self pushViewController:controller animated:NO];
reloadData will call all of your delegate and data source methods, and use them to update the table. If you want a special method call on your table view controller instead, you could change the method declaration to setRootTableViewController:(CustomTableViewController *)controller (or whatever your custom table controller is called), and replace the reloadData line with one that calls that method.
Then, in your app delegate, instead of creating a UINavigationController and adding your custom view controller, create one of these, and call this method to add the first view.
However, if you are using a nib to set the rootViewController, you can just override initWithRootViewController:(UIViewController *)controller, as I imagine that is what the nib will call to set the first view in the stack:
- (id)initWithRootViewController:(UIViewController *)rootViewController {
if ((self = [super init])) {
[(CustomController *)rootViewController doSomethingSpecial];
[self pushViewController:rootViewController];
}
}
Hope this helps!

Resources