2011-09-16 46 views
1
的modalViewController

所以我有一個的UITabBarController應用程序,我想顯示一個登錄頁面,我也這樣做:駁回modalViewController

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDidLogin:) name:UserDidLoginNotification object:nil]; 
LoginViewController* loginViewController = [[LoginViewController alloc] init]; 
     self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0]; 
     [self.tabBarController.selectedViewController presentModalViewController:loginViewController animated:NO]; 
     [loginViewController release]; 

裏面我LoginViewController我也顯示另一個modalViewController:

- (void) twitterLogin: (UIViewController *) askingView 
{ 
    UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _twitter delegate: self]; 

    if (controller) { 
     self.askingView = askingView; 
     [askingView presentModalViewController: controller animated: YES]; 
    } 
} 

我有以下方法,其中askView是LoginViewController, 當我想解僱我這樣做:

[self.askingView dismissModalViewControllerAnimated:YES]; 
    [[NSNotificationCenter defaultCenter] postNotificationName:UserDidLoginNotification object:nil]; 

但是,這並不關閉LoginViewController並顯示UITabBarController視圖..它只是駁回了我的modalViewController顯示從LoginvVIewController。我在這裏做錯了什麼?我也收到以下錯誤:

attempt to dismiss modal view controller whose view does not currently appear. self = <LoginViewController: 0x2aff70> modalViewController = <SA_OAuthTwitterController: 0x2d2a80> 
2011-09-16 09:45:37.750 VoteBooth[4614:707] attempt to dismiss modal view controller whose view does not currently appear. self = <MainViewController: 0x29fec0> modalViewController = <LoginViewController: 0x2aff70> 

回答

13

爲了關閉那些呈現在另一個模式視圖模式的看法,你必須調用dismissModalViewControllerAnimated:父的父。我在我的一些應用程序中使用了它,它對我來說非常合適(經過許多艱苦的努力試圖找出它)。這正是我使用:

[[[self parentViewController] parentViewController] dismissModalViewControllerAnimated:YES]; 
+0

這工作得很好,直到我升級到最新的Xcode和iOS 5上我的iPhone 3GS,有什麼想法? – topace

+1

嘗試更改'presentsViewController'的'parentViewController'引用。這似乎對我有用。 – superjessi

+0

是的你是對的。現在我正在檢查哪個IOS,然後決定要做什麼。 – topace

8
if ([self respondsToSelector:@selector(presentingViewController)]) { 
    [self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:YES]; // for IOS 5+ 
} else { 
    [self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES]; // for pre IOS 5 
} 
2

如果你有一個動態的UX,不知道有多少家長去,你可以用這個遞歸函數弄明白......

- (void) goHome 
{ 
    //Dismiss modal back to home page 
    int numberOfVcsToDismiss = [self findRootViewController:self]; 
    [self dismissToRootVc:numberOfVcsToDismiss]; 
} 

- (int) findRootViewController:(UIViewController*)vc 
{ 
    if(vc) 
    { 
     return 1 + [self findRootViewController:vc.presentingViewController]; 
    } 
    return 0; 
} 

- (void) dismissToRootVc:(int)count 
{ 
    if(count == 1) 
     [self dismissViewControllerAnimated:YES completion:^{}]; 
    if(count == 2) 
     [self.presentingViewController dismissViewControllerAnimated:YES completion:^{}]; 
    if(count == 3) 
     [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:^{}]; 
    if(count == 4) 
     [self.presentingViewController.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:^{}]; 
    if(count == 5) 
     [self.presentingViewController.presentingViewController.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:^{}]; 
    //etc.... 
}