ResignFirstResponder崩溃

时间:2015-02-11 12:22:02

标签: ios objective-c iphone

在尝试NSURLResquest并加载节目之前,我试图关闭键盘......

[self.txtComentario resignFirstResponder]; 

crashes de app ...我已经尝试在loadingThread()内部resignFirstResponder

-(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];
}
编辑:如果我在NSThread之前尝试resignFirstesponder,没有任何反应,没有崩溃但没有键盘Down ..如果我在loadingThread内部尝试...应用程序崩溃

NSThread中的错误是:

  

[UIKeyboardTaskQueue waitUntilAllTask​​sAreFinished]可能只被调用   来自主线程。'

2 个答案:

答案 0 :(得分:1)

有时,异步API请求不会回调主线程上的委托。所以,试着确保你在主线程上。如果您没有使用它,请在对UI进行任何更新之前尝试切换到主线程。

if ([NSThread isMainThread]) {
    NSLog(@"Yes, it is!");
} else {
    NSLog(@"No, it's not. Please switch to main");
}

答案 1 :(得分:0)

尝试替换此代码

[self.view endEditing: YES];