performSelector:withObject:afterDelay:不进行调用

Pad*_*215 17 iphone objective-c ios

在一个方法中,我想在n秒后调用一个方法:

    self.toolBarState = [NSNumber numberWithInt:1];
    [self changeButtonNames];
    [self drawMap];
    [self performSelector:@selector(showActionSheet) withObject:nil afterDelay:2];
Run Code Online (Sandbox Code Playgroud)

我想在drawMap完成后2秒显示操作表.当我使用这个performSelector时,它从不拨打电话.

如果我只是把[self showActionSheet];,它的工作正常.有没有理由说performSelector没有打电话?

编辑:在我的代码的另一部分,我进行相同的调用,它的工作原理:

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];
Run Code Online (Sandbox Code Playgroud)

这里,showActionSheet在drawMap完成后6秒被调用.我猜我的线程还有什么东西我还不明白......

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];

    }
}
Run Code Online (Sandbox Code Playgroud)

EDIT3:

好的,所以方法调用的进度是:

-(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];
}
Run Code Online (Sandbox Code Playgroud)

showActionSheet永远不会被调用.就像我说的,我很确定它是一个线程问题.如果用[self showActionSheet]调用它,它将运行.= /

hat*_*ike 36

我遇到了同样的问题,我必须解决它与接受的答案略有不同.注意我希望我的延迟和选择器是变量?使用块可以让我保持在当前的方法中.

dispatch_async(dispatch_get_main_queue(), ^{
       [self performSelector:loopSelector withObject:nil afterDelay:cycleTime];
});  
Run Code Online (Sandbox Code Playgroud)

顺便说一句,这绝对是一个线程问题.performSelector的文档:withObject:afterDelay:声明这将在延迟后在当前线程上执行,但有时该线程的运行循环不再有效.

可在此处找到关于该主题的更详细讨论


ris*_*shi 21

尝试使用 -

-(void) select {   
    [self changeButtonNames];
    [self drawMap];
    [self performSelectorOnMainThread:@selector(showActionSheet) withObject:nil waitUntilDone:YES];
}
Run Code Online (Sandbox Code Playgroud)

-performSelector:withObject:afterDelay:在同一个线程上调度一个定时器,在传递的延迟后调用选择器.

可能是你的工作 -

-(void) select {   
    [self changeButtonNames];
    [self drawMap];
    [self performSelectorOnMainThread:@selector(someA) withObject:nil waitUntilDone:YES];
}

-(void)someA {
    [self performSelector:@selector(showActionSheet) withObject:nil afterDelay:2];
}
Run Code Online (Sandbox Code Playgroud)