gpt4 book ai didi

iOS Parse Block 结果存储在 UILabel 中

转载 作者:行者123 更新时间:2023-12-01 16:47:05 25 4
gpt4 key购买 nike

我正在使用 Parse iOS SDK,我想存储我的 findObjectsInBackgroundWithBlock : query in a UIL标签 . The UIL标签 is created inside of the viewForHeaderInSection`:我的表格 View 的方法。

如果我创建 UILabel在 block 内部,我可以成功存储我需要的值并在我的 View 上显示标签。当查询在 Parse 中运行时,它会在查询使用本地缓存之前尝试连接到服务器 3 次。如果任何尝试失败或与服务器的连接速度很慢,我的 UILabel闪烁。我不希望这样,所以我的想法是以某种方式在 block 之外访问我的查询结果并将其存储为 UILabel block 完成后。

有人可以告诉我如何在 block 外访问我的查询结果并将结果存储在 UILabel 中吗? ?

下面的示例是我当前如何创建 UILabel在我看来并将我的 block 的结果显示为UILabel .谢谢您的帮助!

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *myHeader = [[UIView alloc] initWithFrame:CGRectMake(0,60,320,20)];
myHeader.backgroundColor = [UIColor grayColor];

PFQuery *query = [PFQuery queryWithClassName:@"ClassName"];
query.cachePolicy = kPFCachePolicyNetworkElseCache;
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

if (!error)
{
NSNumber *sum = [objects valueForKeyPath:@"@sum.sumResults"];
UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(15,0,120,20)] ;
myLabel1.font = [UIFont boldSystemFontOfSize:14.0];
myLabel1.text = [NSString stringWithFormat:@"Sum results: %.1f", [sum floatValue]];
[myHeader addSubview:myLabel1];

}
else
{
NSLog(@"Error: %@ %@", error, [error userInfo]);
}

}];

return myHeader;
}

最佳答案

问题的很大一部分是您在“findObjectsInBackgroundWithBlock”结果(完成) block 中创建标签。因此,除了该代码块之外,您的应用程序中的任何其他地方都不容易使用它。

我建议您将 UILabel 作为 IBOutlet (即将它放入 Storyboard 或 xib 文件并将其连接到 View Controller 中的 socket )。

然后,当您获得想要显示的结果时,post a notification to yourself (or any observers) that it's time to update (我建议
postNotificationName:object:userInfo:
其中 userInfo 字典包含您要添加到该标签的任何信息)。

关于iOS Parse Block 结果存储在 UILabel 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19150491/

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