ios – 以编程方式在appdelegate中创建tabBarController

我一直在关注如何以编程方式添加UITabBarController的不同教程.这很容易使用故事板来实现,但是因为我正在尝试以编程方式学习如何做事我无法做到这一点.目前我已经在didFinishLaunchingWithOptions中获得了这段代码.

tabBarController = [[UITabBarController alloc] init];

NSMutableArray *tabs = [[NSMutableArray alloc] init];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:[[MenuViewController alloc] init]];

[tabBarController setViewControllers:tabs];

[tabs addobject:navController];


[self.window addSubview:tabBarController.view];

编辑代码

tabBarController = [[UITabBarController alloc] init];

MenuViewController *firstTab = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstTab];


self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[navController];
[self.window setRootViewController:tabBarController];
[self.window makeKeyAndVisible];

这对于我的rootViewController调用MenuViewController没有任何意义.我怎样才能实现这一目标?

解决方法

用于5选项卡UITabbarcontroller的贝娄代码尝试使用以下波纹管代码: –

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

anasayfaViewController * firstTab= [[anasayfaViewController alloc] initWithNibName:@"anasayfaViewController" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:firstTab];

SehirRehberiViewController *sehirRehberi = [[SehirRehberiViewController alloc] initWithNibName:@"SehirRehberiViewController" bundle:nil];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:sehirRehberi];

duyuruViewController *duyuru = [[duyuruViewController alloc] initWithNibName:@"duyuruViewController" bundle:nil]; 
UINavigationController *navigationController3 = [[UINavigationController alloc] initWithRootViewController:duyuru];

sikayetViewController *sikayet = [[sikayetViewController alloc] initWithNibName:@"sikayetViewController" bundle:nil];
UINavigationController *navigationController4 = [[UINavigationController alloc] initWithRootViewController:sikayet];

digerViewController *diger = [[digerViewController alloc] initWithNibName:@"digerViewController" bundle:nil];
UINavigationController *navigationController5 = [[UINavigationController alloc] initWithRootViewController:diger];


self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[navigationController1,navigationController2,navigationController3,navigationController4,navigationController5];

 [self.window setRootViewController:tabBarController];
 [self.window makeKeyAndVisible];

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...