iPhone SDK - (void)viewDidLoad { } - 显示进度
我有一个很长的 - (void)viewDidLoad { }
函数,它使用互联网加载页面,每当应用程序加载 URL 时,屏幕就会变黑。我有一个 UIActivityIndicator 设置来显示地址正在加载,但我不知道如何防止它看起来好像应用程序没有响应,并且我不想显示网络活动指示器 [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要在 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.
您也许可以添加一条文字,说明正在加载某些内容。
或者,您也可以使用 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.