gpt4 book ai didi

iphone - 执行选择器 :withObject:afterDelay: not making call

转载 作者:IT王子 更新时间:2023-10-29 08:13:06 25 4
gpt4 key购买 nike

在一个方法中,我想在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];

这里,showActionSheet 在 drawMap 完成 6 秒后被调用。我猜线程中发生了一些我还不明白的事情...

编辑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:

ok,那么方法调用的进度是:

-(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] 调用它,它将运行。 =/

最佳答案

我遇到了同样的问题,因此我解决的问题与公认的答案略有不同。请注意我希望我的延迟和选择器是变量?使用 block 可以让我留在我当前的方法内。

dispatch_async(dispatch_get_main_queue(), ^{
[self performSelector:loopSelector withObject:nil afterDelay:cycleTime];
});

顺便说一句,这绝对是一个线程问题。 performSelector:withObject:afterDelay: 的文档指出这将在延迟后在当前线程上执行,但有时该线程的运行循环不再处于事件状态。

可以找到关于该主题的更详细讨论 here

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

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