问题
I am trying to include a navigation controller within the third tab of my tabbarcontroller. I have had some feedback but am only able to get about this far. The code below doesn't produce any errors, but does not seem to work as the app just quits out. Does anyone have any input on what I might be doing wrong?
UIViewController *viewController1 = [[FirstViewController alloc]
initWithNibName:@"PDCFirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc]
initWithNibName:@"SecondViewController" bundle:nil];
viewController3 = [[UIViewController alloc] initWithNibName:@"ThirdViewController"
bundle:nil];
UINavigationController *navigationcontroller = [[UINavigationController alloc]
initWithRootViewController:viewController3];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray
arrayWithObjects:viewController1,viewController2,navigationcontroller, nil];
Thank you all!
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([PDCAppDelegate class]));
}
}
回答1:
Make sure with all nib names you are using and also with class name, i.e. if you class name of UIViewController is FirstViewController, nib name should be same. And you have used "PDCFirstViewController" for nib name. Same with ThirdViewController & SecondViewController.
Try below code...
FirstViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; ThirdViewController *viewController3 = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil]; UINavigationController *navigationcontroller = [[UINavigationController alloc] initWithRootViewController:viewController3]; self.tabBarController = [[UITabBarController alloc] init]; self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1,viewController2,navigationcontroller, nil];
回答2:
Here is the solution
UIViewController *courseView = [[[CoursesView alloc] initWithNibName:@"CoursesView" bundle:nil] autorelease];
UIViewController *subjectViewController = [[[SubjectViewController alloc] initWithNibName:@"SubjectViewController" bundle:nil] autorelease];
UIViewController *videoViewController = [[[VideoViewController alloc] initWithNibName:@"VideoViewController" bundle:nil] autorelease];
UIViewController *quizViewController = [[[QuizViewController alloc] initWithNibName:@"QuizViewController" bundle:nil] autorelease];
UIViewController *proifileViewController = [[[Profile2ViewController alloc] initWithNibName:@"Profile2ViewController" bundle:nil] autorelease];
UINavigationController *coursNav = [[UINavigationController alloc] initWithRootViewController:courseView];
UINavigationController *subjectNav = [[UINavigationController alloc] initWithRootViewController:subjectViewController];
UINavigationController *videoNav = [[UINavigationController alloc] initWithRootViewController:videoViewController];
UINavigationController *quizNav = [[UINavigationController alloc] initWithRootViewController:quizViewController];
UINavigationController *profileNav = [[UINavigationController alloc] initWithRootViewController:proifileViewController];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = @[coursNav, subjectNav,videoNav,quizNav,profileNav];
self.tabBarController.navigationController.navigationBarHidden=YES;
self.tabBarController.delegate=self;
[self.window addSubview:self.tabBarController.view];
[self.window makeKeyAndVisible];
it works for me
来源:https://stackoverflow.com/questions/13835724/programming-tabbarcontroller-with-navigation-in-appdelegate