2016-01-22 47 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; 
} 

還要確保動作不會被調用兩次。

相關問題