performSelector:withObject:afterDelay:不进行调用 [英] performSelector:withObject:afterDelay: not making call
问题描述
在一个方法中,我想在n秒后调用一个方法:
in a method, i want to call a method after n seconds:
self.toolBarState = [NSNumber numberWithInt:1];
[self changeButtonNames];
[self drawMap];
[self performSelector:@selector(showActionSheet) withObject:nil afterDelay:2];
我想在drawMap完成后2秒显示操作表。当我使用这个performSelector时,它永远不会进行调用。
i want to show the action sheet 2 seconds after drawMap is complete. when i use this performSelector, it never makes the call.
如果我只是放[self showActionSheet];它就可以了。是否有理由为什么performSelector没有进行调用?
if i just put [self showActionSheet];, it works just fine. is there reason why the performSelector is not making the call?
编辑:在我的代码的另一部分,我进行相同的调用并且它有效:
in another part of my code, i make the same call and it works:
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.delegate = (id) self;
[HUD showWhileExecuting:@selector(drawMap) onTarget:self withObject:nil animated:YES];
[self performSelector:@selector(showActionSheet) withObject:nil afterDelay:6];
这里,showActionSheet在drawMap完成后6秒被调用。我猜这个线程还有什么东西我还不明白...
here, the showActionSheet gets called 6 seconds after drawMap has completed. i'm guessing there is something going on with the threads that i don't understand yet...
EDIT2:
-(void)showActionSheet
{
InspectAppDelegate *dataCenter = (InspectAppDelegate *) [[UIApplication sharedApplication] delegate];
if (dataCenter.fieldIDToPass == nil)
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Selected Boundary Options" delegate:(id) self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Analyze a Field",@"Retrieve Saved Analysi", @"Geotag Photos", @"Refresh the map",nil];
actionSheet.tag = 0;
[actionSheet showInView:self.view];
}
else
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Selected Boundary Options" delegate:(id) self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Analyze a Field",@"Retrieve Saved Analysi", @"Geotag Photos", @"Attribute the Field", @"Refresh the map",nil];
actionSheet.tag = 0;
[actionSheet showInView:self.view];
}
}
EDIT3:
好,所以方法调用的进度是:
ok, so the progress of method calls is:
-(void) foundDoubleTap:(UITapGestureRecognizer *) recognizer
{
[HUD showWhileExecuting:@selector(select) onTarget:self withObject:nil animated:YES];
}
-(void) select
{
[self changeButtonNames];
[self drawMap];
[self performSelector:@selector(showActionSheet) withObject:nil afterDelay:2];
}
showActionSheet永远不会被调用。就像我说的,我很确定它是一个线程问题。如果用[self showActionSheet]调用它,它将运行。 = /
showActionSheet never gets called. like i said, i'm pretty sure its a threading issue. if call it with [self showActionSheet], it will run. =/
推荐答案
尝试使用 -
-(void) select {
[self changeButtonNames];
[self drawMap];
[self performSelectorOnMainThread:@selector(showActionSheet) withObject:nil waitUntilDone:YES];
}
-performSelector:withObject:afterDelay:在同一个线程上调度一个计时器来调用传递延迟后的选择器。
-performSelector:withObject:afterDelay: schedules a timer on the same thread to call the selector after the passed delay.
可能是你的工作 -
Might be this work for you -
-(void) select {
[self changeButtonNames];
[self drawMap];
[self performSelectorOnMainThread:@selector(someA) withObject:nil waitUntilDone:YES];
}
-(void)someA {
[self performSelector:@selector(showActionSheet) withObject:nil afterDelay:2];
}
这篇关于performSelector:withObject:afterDelay:不进行调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!