gpt4 book ai didi

ios - ResignFirstResponder 崩溃

转载 作者:行者123 更新时间:2023-11-29 02:18:28 28 4
gpt4 key购买 nike

我正在尝试在执行 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com