Iphone NSXMLParser避免显示/隐藏UILabel
我的课堂上有以下方法:Iphone NSXMLParser避免显示/隐藏UILabel,iphone,objective-c,nsxmlparser,Iphone,Objective C,Nsxmlparser,我的课堂上有以下方法: -(IBAction)loginToAccount:(id)sender { // Display the network activity status indicator [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; // Show the load indicator [self.loadIndicator startAnimating]; self.lo
-(IBAction)loginToAccount:(id)sender {
// Display the network activity status indicator
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
// Show the load indicator
[self.loadIndicator startAnimating];
self.loadIndicator.hidden = NO;
self.loadLabel.hidden = NO;
[usernameTextField resignFirstResponder];
[passwordTextField resignFirstResponder];
[self CheckLoginCredentials];
}
-(void)CheckLoginCredentials {
NSString *APIURL = [[NSString alloc] initWithFormat:@"http://mysite.com/xml.xml"];
NSURL *url = [[NSURL alloc] initWithString:APIURL];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[APIURL release];
[url release];
[xmlParser setDelegate:self];
[xmlParser parse];
}
当我注释[self-CheckLoginCredentials]
时,loadIndicator会显示动画,但当我取消注释[self-CheckLoginCredentials]
时,loadIndicator不会显示,而且用户名文本字段/passwordTextField辞职FirstResponder
不会工作
我做错了什么?谢谢 我认为
-initWithContentsOfURL:
是一个同步url连接,因此会阻止它运行的线程,直到它完成
因此,进度指示器将不会显示,因为它要求它所运行的线程具有活动的运行循环。在主线程上使用同步url连接将阻止该线程上的UI,因此您将看不到进度指示器
正确的方法是使用NSURLConnection的
+ (NSURLConnection *)connectionWithRequest:(NSURLRequest *)request delegate:(id)delegate
只需创建一个封装API请求的NSURLRequest对象,然后在NSURLConnection上将其传递给该方法
然后实现委托回调以获取数据
这种方法的优点是,所有这些都是在一个单独的线程上完成并为您处理的,因此不会阻塞您的UI。刚刚找到了一个解决方法:创建了一个名为“-(iAction)ShowLoader:(id)sender”的新方法,并指向“触碰”甚至用户界面上的按钮。NSXMLParser事件链接到“内部润色”。你知道这是不是一个正确的解决方法吗?谢谢