2012-02-23 26 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]; 

}

相关问题