Reputation: 127
I am beginning iOS programming and I have hit a roadblock. I have a login view controller that redirects the control(after checking user authentication,etc) to a tab controller(homescreen) using the following code:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *viewController = (UITabBarController *)[storyboard instantiateViewControllerWithIdentifier:@"homescreen"];
[self presentViewController:viewController animated:YES completion:nil];
The first view controller in home screen is a navigation controller leading to Subscription View Controller (a Table View Controller). From app delegate.m, I am trying to send an array of values (chefs) which the table can use to populate itself using the following code:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *tabBarController = (UITabBarController *)[storyboard instantiateViewControllerWithIdentifier:@"homescreen"];
UINavigationController *navigationController = [tabBarController viewControllers][0];
SubscriptionViewController *controller = [navigationController viewControllers][0];
controller.chefs = _chefs;
However, the table shows up empty. The NSMutable array "chefs" has been populated appropriately and in the storyboard, the table cell values (UILabels,etc.) have been hooked up correctly to their counterpart values inside Content View. As a check, I NSLogged the [self.chefs count] value inside Subscription view controller:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSLog(@"%d",[self.chefs count]);
return [self.chefs count];
}
However, this returned 0. I suspect, I am not broadcasting the array properly.
EDIT: I removed the login view controller and made the Tab bar controller the root view controller. And then used this piece of code:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UINavigationController *navigationController = [tabBarController viewControllers][0];
SubscriptionViewController *controller = [navigationController viewControllers][0];
controller.chefs = _chefs;
Now I can see the correct cells. I am not sure if the way I am redirecting the control from login view to tab bar controller is correct OR using the way I am using storyboard ID to broadcast the array is correct. Any pointers?
Upvotes: 0
Views: 240
Reputation: 466
Maybe _chefs is being released? Try assigning like this
controller.chefs = [NSArray arrayWithArray:_chefs];
Also make sure the table in controller is reloaded after your assignment. Maybe implement the setter in controller like this:
-(void)setChefs:(NSArray*)chefs
{
_chefs = chefs;
[self.tableview reloadData];
}
Upvotes: 1
Reputation: 53112
It sounds like your array isn't being initialized properly, when you assign it like this:
controller.chefs = _chefs;
Before doing anything with _chefs
make sure it's initialized somewhere:
_chefs = [[NSArray alloc]init];
Upvotes: 1