作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
示例:
[query findObjectsInBackgroundWithBlock:^(NSArray *favObjects, NSError *error) {
for (PFObject *favObject in favObjects) {
dispatch_async(dispatch_get_main_queue(), ^{
[cell setSizeLabel:[_arrayOfSizes objectAtIndex:[[favObject valueForKey:@"size"] integerValue]]];
[[cell sizeDropDownButton] setTitle:[cell sizeLabel] forState:UIControlStateNormal];
});
if ([[[cell currentObject] valueForKey:@"alternativeColour"] boolValue]) {
// Colours are stored in db on parse as numbers, I use array below to determine which colour is being red back by valueForkey below
dispatch_async(dispatch_get_main_queue(), ^{
[cell setColourLabel:[_arrayOfColours objectAtIndex:[[favObject valueForKey:@"colour"] integerValue]]];
[[cell colourDropDownButton] setTitle:[cell colourLabel] forState:UIControlStateNormal];
});
}
[[cell priceLabelSpinner] stopAnimating];
[[cell titleLabelSpinner] stopAnimating];
[[cell sizeDropDownButton] setHidden:NO];
[[cell colourDropDownButton] setHidden:NO];
}
}];
您可以看到我正在主队列上设置按钮标签标题。但是在下面,我将停止在后台队列上旋转以及取消隐藏按钮。这是正确的吗?
最佳答案
任何可能导致您查看更改的东西都应该从主线程调用:
Manipulations to your application’s user interface must occur on the main thread. Thus, you should always call the methods of the UIView class from code running in the main thread of your application. The only time this may not be strictly necessary is when creating the view object itself but all other manipulations should occur on the main thread.
因此,考虑到 Apple 的建议,您应该将代码重写为:
[query findObjectsInBackgroundWithBlock:^(NSArray *favObjects, NSError *error) {
for (PFObject *favObject in favObjects) {
dispatch_async(dispatch_get_main_queue(), ^{
[cell setSizeLabel:[_arrayOfSizes objectAtIndex:[[favObject valueForKey:@"size"] integerValue]]];
[[cell sizeDropDownButton] setTitle:[cell sizeLabel] forState:UIControlStateNormal];
});
if ([[[cell currentObject] valueForKey:@"alternativeColour"] boolValue]) {
// Colours are stored in db on parse as numbers, I use array below to determine which colour is being red back by valueForkey below
dispatch_async(dispatch_get_main_queue(), ^{
[cell setColourLabel:[_arrayOfColours objectAtIndex:[[favObject valueForKey:@"colour"] integerValue]]];
[[cell colourDropDownButton] setTitle:[cell colourLabel] forState:UIControlStateNormal];
});
}
dispatch_async(dispatch_get_main_queue(), ^{
[[cell priceLabelSpinner] stopAnimating];
[[cell titleLabelSpinner] stopAnimating];
[[cell sizeDropDownButton] setHidden:NO];
[[cell colourDropDownButton] setHidden:NO];
});
}
}];
关于ios - 我应该在主线程上启动和停止 UIActivityIndicator 实例吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24714183/
我是一名优秀的程序员,十分优秀!