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

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

我想在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];

在这里,drawMap完成后的6秒钟将调用showActionSheet。我猜想我不了解的线程正在发生一些事情...

编辑2:
-(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];

    }
}

编辑3:

好的,因此方法调用的进度为:
-(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]调用它,它将运行。 =/

最佳答案

尝试使用:

-(void) select {
    [self changeButtonNames];
    [self drawMap];
    [self performSelectorOnMainThread:@selector(showActionSheet) withObject:nil waitUntilDone:YES];
}
-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];
}

关于iphone - performSelector :withObject:afterDelay: not making call,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8746310/

10-13 03:56