我最近将MDProgressHUD添加到我的应用程序中,并按照自述文档中的说明进行操作,该文档建议我在主队列上设置HUD,然后在新线程上执行其他任务,这就是我实现它的方式。这一切都可以在应用程序的初始启动时正常工作,但是如果您选择手机上的主页按钮以便将应用程序放入后台,然后选择应用程序的图标以将其恢复到应用程序崩溃的前景。 / p>
我已经实现了如下代码。当用户选择确定按钮时,应用程序将通过调用Web服务(NSURLConnection),即authenticateUser来验证其登录ID和密码。
- (IBAction)Ok:(id)sender {
[self.txtPassword resignFirstResponder];
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self authenticateUser];
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
}
- (void)authenticateUser {
self.loginEmailAddress = self.txtEmailAddress.text;
self.loginPassword = self.txtPassword.text;
if ([self.loginEmailAddress isEqualToString:@""] || [self.loginPassword isEqualToString:@""]) {
self.lblErrMsg.text = @"Invalid login details. Please try again.";
self.lblErrMsg.hidden = NO;
[MBProgressHUD hideHUDForView:self.view animated:YES];
return;
}
NSString *myRequestString = [NSString stringWithFormat:@"org_id=%@&login_id=%@&pword=%@", signedUpOrgId, self.loginEmailAddress, self.loginPassword];
NSString *myEncodedRequestString = [myRequestString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSData *myRequestData = [myEncodedRequestString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:serviceURL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[theRequest setHTTPMethod: @"POST"];
[theRequest setHTTPBody: myRequestData];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
receivedData = [NSMutableData data];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry! Can't connect." message:@"Sorry, but there seems to be a problem connecting to the internet at the moment. Please try again or wait until you have better reception for a data connection." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
}
日志:
2012-07-17 23:40:33.699 AppName[6155:707] -[ContactListViewController authenticateUser]: unrecognized selector sent to instance 0x2616e0
2012-07-17 23:40:33.704 AppName[6155:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ContactListViewController authenticateUser]: unrecognized selector sent to instance 0x2616e0'
*** First throw call stack:
(0x3146588f 0x377a3259 0x31468a9b 0x31467915 0x313c2650 0x31d78933 0x31439a33 0x31439699 0x3143826f 0x313bb4a5 0x313bb36d 0x33435439 0x30b1ecd5 0xe13c7 0xe136c)
terminate called throwing an exception
我无法弄清楚应用程序第二次崩溃的原因。任何帮助将不胜感激。
答案 0 :(得分:0)
在块中引用self
时,建议通过“弱”变量间接执行此操作(否则您可能永远无法正确释放)。
__弱ContactListViewController * weakself = self; dispatch_after(popTime,dispatch_get_main_queue(),^(void){ [weakself authenticateUser]; [MBProgressHUD hideHUDForView:weakself.view animated:YES]; });
这是因为对你的引用被绑定到那个块;它有一个重要的含义:你肯定你是否已经徘徊了足够长的时间让这个街区有一个很好的参考?您可能需要考虑通过并确保在被调用时“{1}}”的对象“属于”其他人足够长的时间仍然是执行该块的有效实例。