【发布时间】:2013-06-08 14:04:10
【问题描述】:
我的标签不会更新的任何原因?
- (void)setLabel:(NSNotification*)notification {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"setLabel");
[self.label setText:@"My Label"];
});
}
我也尝试过使用 performSelectorOnMainThread 无济于事。
请注意,setLabel 出现在日志中。
附加信息:
我还有另外两个功能,它们做同样的事情,但只是使用不同的文本。其他两个函数没有 dispatch_async 但它们都可以工作。此外,两个工作函数的通知是由 NSURLConnection 发送的(this post 中的方法 #2)。而上述非工作功能的通知是通过调用 FBRequestConnection 发送的(请参阅this post)。
为了清楚起见,我的另外两个工作函数如下:
- (void)setLabel2:(NSNotification*)notification {
NSLog(@"setLabel2");
[self.label setText:@"My Label 2"];
}
- (void)setLabel3:(NSNotification*)notification {
NSLog(@"setLabel3");
[self.label setText:@"My Label 3"];
}
是的,我确实尝试在我的代码中删除 dispatch_async。其实原本没有dispatch_async是因为另外两个都在工作。
【问题讨论】:
-
您是否尝试过暂时移除 dispatch_async 以查看是否设置了标签?
-
确保您使用正确的标签并将其设置为您故事板的正确出口
-
@Kreiri - 如果它是 nil,它不应该让应用程序崩溃吗?拨打该线路后,我的不会崩溃。
-
@Jfuellert - 它最初没有 dispatch_async,我添加它以确保它更新但它没有。
-
向
nil发送消息在Objective-C 中是合法的。它什么也不做。你需要弄清楚为什么self.label为零。
标签: ios uilabel grand-central-dispatch performselector dispatch-async