iPhone SDK - (void)viewDidLoad { } - 显示进度

发布于 2024-08-25 21:11:00 字数 1763 浏览 3 评论 0原文

我有一个很长的 - (void)viewDidLoad { } 函数,它使用互联网加载页面,每当应用程序加载 URL 时,屏幕就会变黑。我有一个 UIActivityIndi​​cator 设置来显示地址正在加载,但我不知道如何防止它看起来好像应用程序没有响应,并且我不想显示网络活动指示器 [[UIApplication sharedApplication] setNetworkActivityIndi​​catorVisible:YES]; 因为屏幕仍然是黑色的,只是在顶角有一个加载程序。我的代码如下:

- (void)viewDidLoad {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    email.text = [defaults objectForKey:@"mom_email"];
    pass.text = [defaults objectForKey:@"mom_pass"];
    if (email.text != nil && pass.text != nil) {
        [self login:loginBtn];
    }
}
- (IBAction)login:(id)sender {
    [email resignFirstResponder];
    [pass resignFirstResponder];
    [loader startAnimating]; // Does nothing though
    NSString *em = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)email.text,NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8);
    NSString *ps = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)pass.text,NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8);
    NSURL *url = [[NSURL alloc] initWithScheme:@"http" host:@"localhost" path:[[NSString alloc] initWithFormat:@"/app/login.php?email=%@&pass=%@", em, ps]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *loginResult = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    [loader stopAnimating];
    NSRange range = [loginResult rangeOfString:@"1"];
    if (range.location != NSNotFound) {
        [self dismissModalViewControllerAnimated:YES]; // Doesn't do anything if function called from viewDidLoad
    }
}

I have a long - (void)viewDidLoad { } function that uses the internet to load a page, and whenever the application loads the URL, the screen is black. I have a UIActivityIndicator setup to show that the address is loading, but I don't know how to prevent it from seeming as if the application is not responding, and I don't want to show the network activity indicator [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; because the screen will still be black, just with a loader in the top corner. My code is below:

- (void)viewDidLoad {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    email.text = [defaults objectForKey:@"mom_email"];
    pass.text = [defaults objectForKey:@"mom_pass"];
    if (email.text != nil && pass.text != nil) {
        [self login:loginBtn];
    }
}
- (IBAction)login:(id)sender {
    [email resignFirstResponder];
    [pass resignFirstResponder];
    [loader startAnimating]; // Does nothing though
    NSString *em = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)email.text,NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8);
    NSString *ps = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)pass.text,NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8);
    NSURL *url = [[NSURL alloc] initWithScheme:@"http" host:@"localhost" path:[[NSString alloc] initWithFormat:@"/app/login.php?email=%@&pass=%@", em, ps]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *loginResult = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    [loader stopAnimating];
    NSRange range = [loginResult rangeOfString:@"1"];
    if (range.location != NSNotFound) {
        [self dismissModalViewControllerAnimated:YES]; // Doesn't do anything if function called from viewDidLoad
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝颜夕 2024-09-01 21:11:00

不要在 viewDidLoad 中阻塞,而是尝试启动一个新线程来执行加载。然后,您只需使用表示正在加载的文本来初始化视图,并在可用时更新任何必要的内容。

Instead of blocking in the viewDidLoad, try starting up a new thread which does the loading. Then you just init the view with text that says things are loading and update whatever content is necessary once it is available.

想挽留 2024-09-01 21:11:00

您也许可以添加一条文字,说明正在加载某些内容。
或者,您也可以使用 NSURLConnection 委托方法来提供进度条,而不仅仅是进度指示器。

You can maybe add a text saying something is loading.
Or you can also play with the NSURLConnection delegate methods to provide a progress bar instead of just a progress indicator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文