将数据从委托传递到 iOS 视图控制器

发布于 2024-12-05 05:38:14 字数 1119 浏览 1 评论 0原文

我试图将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对不⑦ 2024-12-12 05:38:14

您将courseArray 分配给CourseSelectController 的实例,然后立即通过释放该控制器来丢弃该控制器。

然后,您创建一个 WestminsterViewController 并将其指定为窗口的根视图控制器,但该视图控制器从未指定为 courseArray

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.

游魂 2024-12-12 05:38:14

好吧,您在分配 courseArray 后立即释放 courseTimeTableView。

Well you are releasing the courseTimeTableView immediately after you assigned the courseArray.

冷弦 2024-12-12 05:38:14

您的代码存在一些严重问题。

在第二行中,当您使用 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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文