将数据从委托传递到 iOS 视图控制器
我试图将 NSArray 从 appDelegate 传递到 viewController 但似乎数据没有被保留。例如,“courseArray”包含 appDelegate 中的值,但在 viewController 中为空。我做错了什么?
- (BOOL)application:(UIApplication *)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;
}
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:(UIApplication *)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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将
courseArray
分配给CourseSelectController
的实例,然后立即通过释放该控制器来丢弃该控制器。然后,您创建一个
WestminsterViewController
并将其指定为窗口的根视图控制器,但该视图控制器从未指定为courseArray
。You assign
courseArray
to an instance ofCourseSelectController
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 assignedcourseArray
.好吧,您在分配 courseArray 后立即释放 courseTimeTableView。
Well you are releasing the courseTimeTableView immediately after you assigned the courseArray.
您的代码存在一些严重问题。
在第二行中,当您使用 self.courseArray 设置 courseTimeTableView.courseArray 时,self.courseArray 仅返回 nil (假设您使用的是常规合成属性)。
然后在第三行,释放视图控制器!它的保留计数仅为 1,因此已被释放并且不再可用。
从修复这些开始:)
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 :)