ios - 遍历数组时在 iOS 中发布更新标签

标签 ios objective-c arrays loops

我是 iOS 编程的新手,我已经找不到答案了。 在 Xcode 5 中,我遍历一个数组,并尝试在标签发生变化时用值更新它们。

这是 .h 文件...

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) NSArray *currentNumber;
@property (strong, nonatomic) IBOutlet UILabel *showLabel;
- (IBAction)start;
@end 

这里是.m文件的主要部分...

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.currentNumber = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];
}

这就是它变得棘手的地方......

以下工作完美...

- (IBAction)start {
    self.showLabel.text = [NSString stringWithFormat:@"new text"];
}
@end

就像这样...

- (IBAction)start {
    for (NSString *p in self.currentNumber) {
        NSLog(@"%@", p);
        sleep(3);
    }
}
@end

但是当我用设置 .text 属性替换 NSLog 时,它“失败”了。计时仍然发生,标签更新为数组中的最后一项......

- (IBAction)start {
    for (NSString *p in self.currentNumber) {
        self.showLabel.text = [NSString stringWithFormat:@"%@", p];
        sleep(3);
    }
}
@end

最后一点奇怪的是,如果我使用 NSLog,并尝试在调用“for”循环之前更改 .text 属性,则文本更改将被忽略,直到循环完成之后...

- (IBAction)start {
    self.showLabel.text = [NSString stringWithFormat:@"5"];
    for (NSString *p in self.currentNumber) {
        NSLog(@"%@", p);
        sleep(3);
    }
}
@end

我错过了什么?

(如果您想查看源文件,可以在https://github.com/lamarrg/iterate 获取它们

最佳答案

如您所知,UI 只会在主线程处理事件时更新。在一个循环中,它不会。

有几种解决方法。

最简单的方法是在后台线程中执行循环。但是有一个问题:这将允许用户继续与您的 UI 交互。而且,UI 只能从主线程更新。

您需要将您的工作分派(dispatch)到后台,然后让后台将您的工作分派(dispatch)回主线程。

这听起来很复杂,确实如此。值得庆幸的是,Apple 向 Objective-C 添加了 block 和 Grand Central Dispatch。您可以使用它们来分解代码块并确保它们在正确的线程上执行。

- (IBAction)start {
    [self disableMyUI];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_NORMAL, 0), ^{
        // this code will be executed "later", probably after start has returned.
        // (in all cases, later should be considered "soon but not immediately.")
        for (NSString *p in self.currentNumber) {
            dispatch_async(dispatch_get_main_queue(),^{
                // this code will be executed "later" by the main loop.
                // You may have already moved on to the next thing, and even
                // dispatched the next UI update.
                // Don't worry; the main queue does things in order.
                self.showLabel.text = [NSString stringWithFormat:@"%@", p];
            });
            sleep(3); // do your heavy lifting here, but keep in mind:
                      // you're on a background thread.
        }
        dispatch_async(dispatch_get_main_queue,^{
            // this occurs "later," but after other all other UI events queued
            // to the main queue.
            [self enableMyUI];
        });
    }
    // this line of code will run before work is complete
}

您必须编写disableMyUIenableMyUI;确保它们禁用所有内容(如果您使用导航,则包括后退按钮,如果您使用标签栏 Controller ,则包括标签栏等)。

另一种解决方法是使用 NSTimer。但是,如果你这样做,你仍然在主线程上做你的工作。如果您可以将您的工作分成可预测的小块,它会起作用,但您最好在后台线程上进行。

要记住一件事:虽然您在开发时不太可能遇到问题,但在主线程上执行繁重的工作导致用户崩溃。在 iOS 上,有一个进程可以监视应用程序是否响应事件,例如绘图更新。如果应用程序没有及时响应事件,它将被终止。因此,忍受缺乏 UI 更新不是您的选择;您只需要从后台线程执行耗时的操作。

另见:

关于ios - 遍历数组时在 iOS 中发布更新标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19671030/

相关文章:

ios - 获取企业 IOS 应用程序的 info.plist

objective-c - Objective C 定义属性的问题

iphone - 如何使 UITableView 上的单元格不可选?

c++ - memcpy 如何处理指向数组的指针?

java - 如何将字符串转换为二维数组 (java)(GameBoard)

iOS 人机界面指南 : Picker Control on iPad without Popover

objective-c - AudioUnit 音调生成器在生成的每个音调结束时都会发出一声鸣叫

重新加载 JS 时,iOS React Native 应用程序在 JSCExecutor.cpp 中崩溃

iOS - UITableView 删除一个部分中的所有行

C++通过for循环插入数组