且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在iPhone上启动应用程序之前显示等待的进度栏

更新时间:2023-01-25 21:53:18

您不能.必须先启动应用程序,然后才能显示任何动态内容.加载第一个ViewController之后,您当然可以显示进度条并进行加载.在此之前,您只能显示静态图片Default.png&默认值@ 2x.png.如果您将繁重的工作延迟到-[ViewController viewDidLoad] -方法完成之后,您就可以在进行繁重的工作时显示任何类型的GUI.

You can't. The application needs to be started before you can show any dynamic content. After your first ViewController is loaded you can of course show a progress bar and do loading. Before that, you can only show a static image Default.png & Default@2x.png. If you delay the heavy loading to after your -[ViewController viewDidLoad]-method is finished you can show any kind of GUI while doing the heavy work.

本示例将在您的viewDidLoad中分离一个新线程并进行一些繁重的工作(在本例中为睡眠1秒),并更新一个 UIProgressView 并在睡眠10次时记录进度,将progressview的值增加0.1每次.

This example will detach a new thread in your viewDidLoad and do some heavy work (in this case sleep for 1 second) and update a UIProgressView and log the progress as it sleeps 10 times, incrementing the progressview with 0.1 each time.

- (void)viewDidLoad
{
 // .....
   [NSThread detachNewThreadSelector:@selector(workerBee) toTarget:self withObject:nil];
}

- (void)workerBee
{
    NSAutoreleasePool* workerPool = [[NSAutoreleasePool alloc] init];

    // Put your image loading between this line and [workerpool release]

    for (float i = 0; i<1; i+= 0.1)
    {
        [self performSelectorInBackground:@selector(updateProgress:) withObject:[NSNumber numberWithFloat:i]];
        sleep(1000);
    }
    [workerPool release];
}

- (void)updateProgress:(NSNumber*)number
{
    NSLog("Progress is now: %@", number);
    [progressView setProgress:[number floatValue]];
}