什么是阻止动作两次射击的更有效方法?

Cai*_*men 1 objective-c ios

当有人在导航栏上点击rightBarButtonItem并快速再次点击程序崩溃时,我遇到了问题.崩溃是可以理解的,因为新视图尚未完成加载,并且按钮仍然可见再次点击,因此它将尝试再次推动视图崩溃程序.我已经尝试了几种方法来尝试防止这种情况并且我当前的实现有些工作,但我知道有一个更好的解决方案,也许是一个内置于框架中的解决方案?

- (void) loadView
{
    [super loadView];

    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
                                               initWithTitle:@"Members" 
                                               style:UIBarButtonItemStylePlain 
                                               target:self 
                                               action:@selector(showRoster)] autorelease];
}


-(void) showRoster {
    if (seconds + 3 < [[NSDate date] timeIntervalSince1970]) { //This is where I am trying to prevent the button from being activated twice.
        seconds = [[NSDate date] timeIntervalSince1970];

        vcRoster = [[RosterDataViewController alloc] init];
        vcRoster.rosterDataModel.group_id = self.tweetsByGroupIdModel.group_id;
        [self.navigationController pushViewController:vcRoster animated:YES];
    }
Run Code Online (Sandbox Code Playgroud)

}

我也尝试过这个,但它没有做我认为应该做的事情.

if (!self.navigationController.isBeingPresented)
Run Code Online (Sandbox Code Playgroud)

我已经尝试解决问题并用谷歌搜索答案,但我还没有找到解决这个问题的好方法.我知道上面的事情不是一个很好的做事方式,我愿意接受任何改进的建议,因为我对IOS世界非常陌生.在此先感谢您的帮助.

ser*_*gio 6

运行操作方法后,请立即尝试禁用该按钮:

-(void) showRoster:(UIButton*)sender {
  sender.enabled = NO;
  ...
}
Run Code Online (Sandbox Code Playgroud)

您可以稍后启用它,以使其再次响应.如何最好地做到这一点取决于您显示的视图:如果它是全屏,您可能只是重新启用按钮viewDidAppear:(即,在视图消失后,您的按钮再次可见).