ios - ResignFirstResponder 崩溃

标签 ios objective-c iphone

我正在尝试在执行 NSURLResquest 和加载节目之前按下键盘...

[self.txtComentario resignFirstResponder]; 

应用程序崩溃...我也已经尝试在 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,什么也没有发生,没有崩溃但键盘按下没有。如果我尝试在 loadingThread 中...应用程序崩溃

NSThread 内部的错误是:

[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'

最佳答案

有时,异步 API 请求不会回调到主线程上的委托(delegate)。因此,请尝试确保您位于主线程上。如果您不在其中,请在对 UI 进行任何更新之前尝试切换到主线程。

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

关于ios - ResignFirstResponder 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28454254/

相关文章:

ios - 如何修复错误 : Binary operator '==' cannot be applied to operands of type 'NSExpression.ExpressionType' and '_'

ios - React Native FBSDK 登录 - 使用应用程序登录失败

ios - 如何更改 iCarousel 库的包装属性?

objective-c - 确定窗口是否可调整大小

iphone - 为什么地址词典中没有邮政编码?

ios - 通过广告跟踪 Facebook 应用安装

iphone - 无法正确设置 NSURLIsExcludedFromBackupKey

ios - NSURLProtocol canInitWithRequest 调用 5 次

iphone - pushViewController 在 iOS 5 上不起作用(在 iOS 6 上正常)

iphone - 如何设置一个 NSPredicate 来使用 NSFetchedResultsController 过滤指定组中的项目