在我的应用中有一个发布选项,用户填写信息并将其发送到我的网络服务。
我在模态视图中有“表单”。当触摸按钮取消时,我用它来关闭它。
当触摸发布按钮时,我启动一个线程,而ws正在使用中。
我希望在ws操作结束时调用我的取消功能。
发布按钮:
self.myThread = [[NSThread alloc]initWithTarget:self selector:@selector(startThread) object:nil];
[self.myThread start];
startThread:
[self.ws publicar:self.registro];
[self.myThread performSelectorOnMainThread:@selector(Cancelar) withObject:nil waitUntilDone:NO];
[self.myThread cancel];
Cancelar:
-(IBAction)Cancelar{
[SingletonTelasAdicionar resetar];
[self dismissViewControllerAnimated:YES completion:nil];
}
当我触摸发布按钮时,我得到了这个:
2014-04-02 20:11:25.540 PetFinder [5356:60b] - [NSThread Cancelar]: 无法识别的选择器发送到实例0x112100030 2014-04-02 20:11:25.541 PetFinder [5356:60b] *由于未被捕获而终止应用程序 异常'NSInvalidArgumentException',原因:' - [NSThread Cancelar]: 无法识别的选择器发送到实例0x112100030' * 第一次抛出调用堆栈:(0 CoreFoundation 0x0000000101b6f495 exceptionPreprocess + 165 1 libobjc.A.dylib
类型的未捕获异常
0x000000010184799e objc_exception_throw + 43 2 CoreFoundation
0x0000000101c0065d - [NSObject(NSObject)doesNotRecognizeSelector:] + 205 3 CoreFoundation 0x0000000101b60d8d __ 转发 + 973 4 CoreFoundation 0x0000000101b60938 _CF_forwarding_prep_0 + 120 5基础
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图形服务
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:终止于 NSException(lldb)
答案 0 :(得分:2)
崩溃看起来很简单:self.myThread是一个NSThread。它没有实现Cancelar
。实现它的事情是自我。试试 -
[self performSelectorOnMainThread:@selector(Cancelar) withObject:nil waitUntilDone:NO];
围绕它的设计可能还需要一些关注。如果视图控制器显示表单,那么如何执行此操作:
// 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:^{}];
}
}];
}
这会从主线程运行请求。当它完成时,它在主线程上运行完成块。在那里你解雇了提供表单的视图控制器。取消将如下所示:
- (IBAction)cancelAction:(id)sender {
[self dismissViewControllerAnimated:YES completion:^{}];
}
答案 1 :(得分:0)
这种功能需要适当的参数。
你的应该是:
-(IBAction)Cancelar:(id)sender {
[SingletonTelasAdicionar resetar];
[self dismissViewControllerAnimated:YES completion:nil];
}
此外,您应该将选择器调用更改为:
[self.myThread performSelectorOnMainThread:@selector(Cancelar:) withObject:nil waitUntilDone:NO];
编辑:格式化