gpt4 book ai didi

ios - 使用 PerformSelectorOnMainThread 关闭 uiview

转载 作者:行者123 更新时间:2023-11-29 10:44:30 24 4
gpt4 key购买 nike

在我的应用程序中有一个发布选项,用户可以在其中填写信息并将其发送到我的网络服务。

我在模态视图上有“表单”。当触摸取消按钮时,我用来关闭它的功能。

当发布按钮被触摸时,我启动一个线程,而 ws 正在使用中。

我想在 ws 操作结束时调用我的 Cancel 函数。

发布按钮:

 self.myThread = [[NSThread alloc]initWithTarget:self selector:@selector(startThread) object:nil];
[self.myThread start];

开始线程:

    [self.ws publicar:self.registro];
[self.myThread performSelectorOnMainThread:@selector(Cancelar) withObject:nil waitUntilDone:NO];
[self.myThread cancel];

取消:

-(IBAction)Cancelar{

[SingletonTelasAdicionar resetar];
[self dismissViewControllerAnimated:YES completion:nil];
}

当我点击发布按钮时,我得到了这个:

2014-04-02 20:11:25.540 PetFinder[5356:60b] -[NSThread Cancelar]: unrecognized selector sent to instance 0x112100030 2014-04-02 20:11:25.541 PetFinder[5356:60b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSThread Cancelar]: unrecognized selector sent to instance 0x112100030' * First throw call stack: ( 0 CoreFoundation 0x0000000101b6f495 exceptionPreprocess + 165 1 libobjc.A.dylib
0x000000010184799e objc_exception_throw + 43 2 CoreFoundation
0x0000000101c0065d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 3 CoreFoundation 0x0000000101b60d8d __forwarding
+ 973 4 CoreFoundation 0x0000000101b60938 _CF_forwarding_prep_0 + 120 5 Foundation
0x000000010144ca17 NSThreadPerformPerform + 227 6 CoreFoundation 0x0000000101afed21 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 7 CoreFoundation 0x0000000101afe5f2 __CFRunLoopDoSources0 + 242 8 CoreFoundation 0x0000000101b1a46f __CFRunLoopRun + 767 9 CoreFoundation
0x0000000101b19d83 CFRunLoopRunSpecific + 467 10 GraphicsServices
0x0000000102cedf04 GSEventRunModal + 161 11 UIKit
0x00000001003f4e33 UIApplicationMain + 1010 12 PetFinder
0x0000000100008fc3 main + 115 13 libdyld.dylib
0x000000010381e5fd start + 1 14 ???
0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

最佳答案

崩溃看起来很简单:self.myThread 是一个 NSThread。它没有实现 Cancelar。实现它的是 self 。尝试-

[self performSelectorOnMainThread:@selector(Cancelar) withObject:nil waitUntilDone:NO];

围绕它的设计可能也需要一些注意。如果 View Controller 呈现表单,如何执行此操作:

// on the vc that presents the 'form'
- (IBAction)submitAction:(id)sender {

NSMutableURLRequest *request = // ... build a request to post your data
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

if (!error) {
[self dismissViewControllerAnimated:YES completion:^{}];
}
}];
}

这会在主线程之外运行请求。完成后,它会在主线程上运行完成 block 。在那里你关闭了呈现表单的 View Controller 。取消看起来像这样:

- (IBAction)cancelAction:(id)sender {
[self dismissViewControllerAnimated:YES completion:^{}];
}

关于ios - 使用 PerformSelectorOnMainThread 关闭 uiview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22824971/

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