2016-01-22 45 views
0

我有一个UIButton在我的应用程序,当按下它显示下一个视图控制器。有时,由于后台进程,UI锁定并且应用程序冻结一段时间。当发生这种情况时,用户可能会多次点击该按钮,因为第一次敲击时没有立即发生任何事情,并且当发生这种情况时,UINavigationController再次将ViewController推到自己的顶部,因此您必须多次返回以获取回到家里。这里是我的代码:UINavigationController推几个相同的viewController

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.pushVCButton.multipleTouchEnabled = NO; 
} 

- (IBAction)pushVCButtonPressed:(id)sender { 
    self.pushVCButton.enabled = NO; 
    ViewController *viewController = [[ViewController alloc] init]; 
    [self.navigationController pushViewController:viewController animated:YES]; 
    self.pushVCButton.enabled = YES; 
} 

我如何得到这个永远不会推多个viewController实例?

回答

4

你真的应该尽量使后台进程不能在UI线程中运行,但如果你不能尝试设置启用是按钮,只有当视图没有消失或监听推动画完成:

- (IBAction)pushVCButtonPressed:(id)sender { 
    self.pushVCButton.enabled = NO; 
    ViewController *viewController = [[ViewController alloc] init]; 
    [self.navigationController pushViewController:viewController animated:YES]; 
    // Hack: wait for this view to disappear to enable the button 
    //self.pushVCButton.enabled = YES; 
} 

- (void)viewDidDisappear:(BOOL)animated { 
    [super viewDidDisappear:animated] 
    self.pushVCButton.enabled = YES; 
} 

还要确保动作不会被调用两次。

相关问题