首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ResignFirstResponder崩溃

ResignFirstResponder崩溃
EN

Stack Overflow用户
提问于 2015-02-11 12:22:01
回答 2查看 417关注 0票数 0

我试着在做NSURLResquest和加载节目之前先放下键盘.

代码语言:javascript
复制
[self.txtComentario resignFirstResponder]; 

崩溃de app.我也尝试过在loadingThread()中使用

代码语言:javascript
复制
-(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只能从主线程调用。

EN

回答 2

Stack Overflow用户

发布于 2015-02-11 13:37:55

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

代码语言:javascript
复制
if ([NSThread isMainThread]) {
    NSLog(@"Yes, it is!");
} else {
    NSLog(@"No, it's not. Please switch to main");
}
票数 1
EN

Stack Overflow用户

发布于 2015-02-11 12:27:26

尝试用以下代码替换

代码语言:javascript
复制
[self.view endEditing: YES];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28454254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档