我试着在做NSURLResquest和加载节目之前先放下键盘.
[self.txtComentario resignFirstResponder]; 崩溃de app.我也尝试过在loadingThread()中使用
-(void) loadingThread {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
[self.myLoadingView setFrame:CGRectMake(0, 0, 320, 568)];
[self.myLoadingImagem setFrame:CGRectMake(133, 250, 54, 9)];
[appDelegate.window addSubview:self.myLoadingView];
[appDelegate.window addSubview:self.myLoadingImagem];
[self.myLoadingView setHidden:NO];
[self animar];
}
-(IBAction)btnComentarClick:(id)sender {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
[self.txtComentario resignFirstResponder];
[NSThread detachNewThreadSelector:@selector(loadingThread) toTarget:self withObject:nil];
NSString* comentarios = [kBaseURL stringByAppendingPathComponent:kComentarios];
NSURL* url = [NSURL URLWithString:comentarios]; //1
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST"; //2
// NSDictionary *usuarioDict = [[NSDictionary alloc] initWithObjectsAndKeys:txtEmailCadastro.text, @"email", txtSenhaCadastro.text, @"senha", categorias, @"categorias", nil];
NSMutableDictionary* jsonable = [NSMutableDictionary dictionary];
safeSet(jsonable, @"idUsuario", appDelegate.usuarioLogado.identificador);
safeSet(jsonable, @"nomeUsuario", appDelegate.usuarioLogado.nome);
safeSet(jsonable, @"textoComentario", self.txtComentario.text);
safeSet(jsonable, @"idOcorrencia", _location._id);
NSData* data = [NSJSONSerialization dataWithJSONObject:jsonable options:0 error:NULL]; //3
request.HTTPBody = data;
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; //4
NSURLSessionConfiguration* config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession* session = [NSURLSession sessionWithConfiguration:config];
NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { //5
if (!error) {
// NSArray* responseArray = @[[NSJSONSerialization JSONObjectWithData:data options:0 error:NULL]];
NSLog(@"comentado");
dispatch_async(dispatch_get_main_queue(), ^(void){
self.txtComentario.text = @"";
[self.txtComentario resignFirstResponder];
});
}
}];
[dataTask resume];
[self listarComentarios];
}编辑:如果我在resignFirstesponder之前尝试NSThread,什么都不会发生,没有崩溃,但没有键盘Down..If,我尝试在loadingThread...the应用程序崩溃。
NSThread内部的错误是:
UIKeyboardTaskQueue waitUntilAllTasksAreFinished只能从主线程调用。
发布于 2015-02-11 13:37:55
有时,异步API请求不会调用主线程上的委托。所以,试着确保你在主线程上。如果您不在上面,那么在对UI进行任何更新之前,请尝试切换到主线程。
if ([NSThread isMainThread]) {
NSLog(@"Yes, it is!");
} else {
NSLog(@"No, it's not. Please switch to main");
}发布于 2015-02-11 12:27:26
尝试用以下代码替换
[self.view endEditing: YES];https://stackoverflow.com/questions/28454254
复制相似问题