2012-02-23 45 views
1

在我的應用程序委託中,我在我的選項卡上加載了一個視圖控制器。該控制器上有三個按鈕,一個用於導航到每個選項卡。當按下第二個按鈕時,我想關閉視圖控制器並轉到第二個選項卡。但這似乎並不正常。從ModalViewController中選擇第二個選項卡

我的AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{  
    //-- Insert a delay of 5 seconds before the splash screen disappears 
    [NSThread sleepForTimeInterval:3.0];   

    // Set the tab bar controller as the window's root view controller and display. 
    self.window.rootViewController = self.tabBarController; 

    // Set StartView to load first 
    StartViewController *startViewController = [[StartViewController alloc] initWithNibName:@"StartView" bundle: nil]; 
    [window addSubview: [startViewController view]]; 
    [window makeKeyAndVisible]; 

    [self.tabBarController presentModalViewController:startViewController animated:NO]; 
    [startViewController release]; 

    return YES; 
} 

,這裏是我目前的IBAction爲,這似乎不工作:

- (IBAction) toSecondView:(id)sender 
    { 
    // Show status bar 
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone]; 

    [(UITabBarController *)self.parentViewController setSelectedIndex:1]; 

    [self dismissModalViewControllerAnimated:NO]; 
} 

我想這些也沒有成功:

self.tabBarController.selectedIndex = 1; 

and

[self.tabBarController setSelectedIndex:1]; 

任何人都可以幫我解釋我失蹤的事嗎?

+0

它happend因爲,你已經添加視圖 - 控制到窗口子視圖,然後呈現的是對的viewController tabBarController – Kamarshad 2012-02-27 11:03:56

回答

0

這種情況是因爲低於原因。

您已將ViewController添加到窗口中作爲子視圖,不需要添加SubView,因爲您已經將該ViewController作爲ModalViewController呈現。

請嘗試以下操作。

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{  
//-- Insert a delay of 5 seconds before the splash screen disappears 
[NSThread sleepForTimeInterval:3.0];   

// Set the tab bar controller as the window's root view controller and display. 
self.window.rootViewController = self.tabBarController; 

// Set StartView to load first 
StartViewController *startViewController = [[StartViewController alloc] initWithNibName:@"StartView" bundle: nil]; 
//[window addSubview: [startViewController view]]; no need to add subView here 
[window makeKeyAndVisible]; 

[self.tabBarController presentModalViewController:startViewController animated:NO]; 
[startViewController release]; 
return YES; 

}

-(IBAction) toSecondView:(id)sender 
{ 
// Show status bar 
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone]; 
//create delegate's class object for accessing tabBarController 
AppDelegate* delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate]; 
//instead of [(UITabBarController *)self.parentViewController setSelectedIndex:1]; 
//delegate.tabBarController your tabBarControler at which you have added viewController 
[delegate.tabBarController setSelectedIndex:1]; 

[self dismissModalViewControllerAnimated:NO]; 

}

相關問題