gpt4 book ai didi

ios - 从 ViewController 中的 AppDelegate 访问数据。

转载 作者:行者123 更新时间:2023-11-28 22:12:08 25 4
gpt4 key购买 nike

我是一名初级程序员,也是这个网站的新手,我在提问之前尝试寻找解决方案,但在措辞上遇到了麻烦,如果之前有人问过这个问题,我深表歉意。

我正在使用 Parse,基本上我现在的主要目标是仅在标签中显示应用中的用户数量。

在我的 AppDelegate.m 中

PFQuery *userCountQuery = [PFUser query];
[userCountQuery countObjectsInBackgroundWithBlock:^(int userCount, NSError *error) {
if (!error) {
// The count request succeeded. Log the count
NSLog(@"There are %d users", userCount);
} else {
// The request failed
}
}];

该代码在我的控制台中获得了正确的数字,现在我只面临将此变量放入 View Controller 以与标签一起使用的挑战。有没有一种简单的方法可以做到这一点,或者我的方法从一开始就存在缺陷?

最佳答案

我假设目前您在 AppDelegate 中有一个方法和您的片段,就像这样:

@interface AppDelegate {
...

- (void)countUser;
}

然后您可以将该方法更改为:

- (void)countUserWithSuccessfulBlock:(void (^)(int))successfulBlock
{
PFQuery *userCountQuery = [PFUser query];
[userCountQuery countObjectsInBackgroundWithBlock:^(int userCount, NSError *error) {
if (!error) {
// The count request succeeded. Log the count
NSLog(@"There are %d users", userCount);
successfulBlock(userCount);
} else {
// The request failed
}
}];
}

然后从您的 ViewController:

AppDelegate* appDelegate = (AppDelegate*) [UIApplication shareApplication].delegate;
[appDelegate countUserWithSuccessfulBlock:^(int result) {
//Display to your label;
}];

关于ios - 从 ViewController 中的 AppDelegate 访问数据。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22624800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com