Passing data from delegate to viewcontroller iOS
i'm trying to pass an NSArray from the appDelegate to the viewController but it seems that the data is not being retained. E.g. 'courseArray'contains values in the appDelegate but in the viewController its empty. What am i doing wrong?
- (BOOL)application:(UIApp开发者_StackOverflow中文版lication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
CourseSelectController *courseTimeTableView = [[CourseSelectController alloc] initWithNibName:nil bundle:nil];
courseTimeTableView.courseArray = self.courseArray;
[courseTimeTableView release];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[WestminsterViewController alloc] init] autorelease];
if([self.window respondsToSelector:@selector(setRootViewController:)])
{
[self.window performSelector:@selector(setRootViewController:) withObject:self.viewController];
}
else
{
[self.window addSubview:[self.viewController view]];
[self.viewController.view setFrame:[self.window bounds]];
}
[self.window makeKeyAndVisible];
return YES;
}
You assign courseArray
to an instance of CourseSelectController
and then immediately throw away that controller by releasing it.
Then, you create a WestminsterViewController
and assign it as your window's root view controller, but that view controller was never assigned courseArray
.
Well you are releasing the courseTimeTableView immediately after you assigned the courseArray.
There are some serious issues with your code.
In the second line, when you set courseTimeTableView.courseArray with self.courseArray, self.courseArray just returns nil (assuming you are using regular synthesized properties).
Then in the third line, you release the view controller! It only had a retain count of 1, so it is deallocated and no longer usable.
Start by fixing these :)
精彩评论