Just beginning iOS development and I am getting a crash on creating an UITabBarController with a TableViewController in tab 1 (2nd tab).
Here is my code – AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
diseases = [NSMutableArray arrayWithCapacity:5];
Disease *disease1 = [[Disease alloc] init];
Disease *disease2 = [[Disease alloc] init];
Disease *disease3 = [[Disease alloc] init];
Disease *disease4 = [[Disease alloc] init];
Disease *disease5 = [[Disease alloc] init];
disease1.name = @"disease1";
disease2.name = @"disease2";
disease3.name = @"disease3";
disease4.name = @"disease4";
disease5.name = @"disease5";
[diseases addObject:disease1];
[diseases addObject:disease2];
[diseases addObject:disease3];
[diseases addObject:disease4];
[diseases addObject:disease5];
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UINavigationController *navigationController =
[[tabBarController viewControllers] objectAtIndex:0];
FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:@"NormalBiopsy" bundle:nil];
firstViewController = [[tabBarController viewControllers] objectAtIndex:0];
DiseaseTableViewController *diseaseTableViewController = [[DiseaseTableViewController alloc] initWithNibName:@"DiseaseTableViewController" bundle:nil];
diseaseTableViewController = [[tabBarController viewControllers] objectAtIndex:1];
SecondViewController *secondViewController = [[SecondViewController alloc]
initWithNibName:@"TestYourSelf" bundle:nil];
secondViewController = [[tabBarController viewControllers] objectAtIndex:2];
ThirdViewController *thirdViewController = [[ThirdViewController alloc] initWithNibName:@"About" bundle:nil];
thirdViewController = [[tabBarController viewControllers] objectAtIndex:3];
// Override point for customization after application launch.
return YES;
}
This leads to a UITabBarController with a blank table in the TableView of the DiseaseTableViewController – because no data populated.
But if I do
diseaseTableViewController.diseases = diseases;
the app crashes with:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [UINavigationController setDiseases:]: unrecognized selector sent to instance
So I tried to add the diseaseTableViewController to the navigationController:
diseaseTableViewController = [[navigationController viewControllers] objectAtIndex:1];
Which causes:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FirstViewController viewControllers]: unrecognized selector sent to instance
I tried populating the DiseaseTableViewController in that class implementation rather than the AppDelegate but ended with a blank table in the UI again.
Any ideas?
I resolved this by leaving the original AppDelegate as is, and populating the data of DiseaseController.diseases in the viewDidLoad() kind of like what @Zhang mentioned