在我的app委托中,我在我的tabbar上加载了一个视图控制器。该控制器上有三个按钮,一个用于导航到每个选项卡。按下第二个按钮时,我想关闭视图控制器并转到第二个选项卡。但这似乎不正常。
我的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;
和
[self.tabBarController setSelectedIndex:1];
任何人都可以帮助我并解释我缺少的东西吗?
答案 0 :(得分: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];
}