2012-01-13 87 views
0

我在MainWindow.xib上添加了一個tabbar控制器,它顯示5個選項卡並使用選項卡欄控制器的委託方法:shouldSelectViewController在我的應用程序委託,它返回布爾值(是或否)。從alertview委託方法返回一個BOOL值: - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

在此代理方法中,我向用戶顯示警告(如果用戶從標籤1到其他標籤)。此警報包含2個按鈕:確定和取消。

如果用戶單擊確定,那麼我希望委託方法返回YES(以便用戶可以轉到其他選項卡),並且如果用戶選擇了取消(如果他只想保留在選項卡1上),則我要該方法返回NO。

所以,基本上我想讓shouldSelectViewController方法停止執行,直到屏幕上出現時間警報。有什麼辦法可以從我的alert view的委託方法返回一個BOOL,而這個方法又可以由shouldSelectViewController或任何可能適用於這種情況的線程解決方案返回?

回答

2

試試這個

在.H

UIViewController *tmpController; 

在.M

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController { 
tmpController = viewController; 
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 
[alert show]; 
[alert release]; 
return NO; 
} 

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
if (buttonIndex) { 
    self.tabBarController.selectedViewController = tmpController; 
} 
} 
+0

天才伊戈爾。它工作的人。非常感謝。 :) – anshul 2012-01-13 08:05:34

+0

沒什麼。 :) – 2012-01-13 08:06:34