结合 UITabController 和UINavigation 控制器以编程方式 http://t.co/R52RlUL UITabController &以编程方式进行 UINavigation 控制器
我已经创建了一个 UItabbarcontroller 和 2 个带有 UITableViews 的视图,仅使用代码(没有 IB 的东西),我现在想在顶部添加一个导航栏,其中包括添加和编辑按钮,但是我似乎被绊倒并炸毁了我的应用程序或仅将导航控制器添加到第三个选项卡。
这是我添加选项卡栏和切换视图的主要代码
仅供参考 - 我正在使用 XCode4
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.mainTabBar = [[UITabBarController alloc] init];
// create the 2 views
tableViewController* vc1 = [[tableViewController alloc] init];
tableViewController2* vc2 = [[tableViewController2 alloc] init];
// put them in an array
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil];
// for the tab bar
mainTabBar.viewControllers = controllers;
// Add the tab bar controller's current view as a subview of the window
[self.window addSubview:self.mainTabBar.view];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
}
I have created a UItabbarcontroller and 2 views with UITableViews just using code (no IB stuff) and I want to now add a navigation bar at the top that will include add and edit buttons, however I seem to be tripping up and blowing my app up or adding navgation controller to a 3rd tab only.
Here is my main code for adding the tab bar and switching views
FYI - I am using XCode4
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.mainTabBar = [[UITabBarController alloc] init];
// create the 2 views
tableViewController* vc1 = [[tableViewController alloc] init];
tableViewController2* vc2 = [[tableViewController2 alloc] init];
// put them in an array
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil];
// for the tab bar
mainTabBar.viewControllers = controllers;
// Add the tab bar controller's current view as a subview of the window
[self.window addSubview:self.mainTabBar.view];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要哪里的导航控制器?您必须为 UITabBarController 中想要的每个选项卡创建一个。
您添加一个 navigationController 及其堆栈上的第一个视图控制器。试试这个:
另请注意,您需要释放您分配或保留的任何内容。您可以像我一样,在初始化它们时添加
autorelease
,也可以在将它们添加到controllers
数组后显式释放它们。然后,您可以在其
loadView
或viewDidLoad
方法中为每个视图控制器配置navigationItem
,具体取决于您的实现方式。Where do you want navigation controller(s)? You have to create one for each tab you want one in the UITabBarController.
You add a navigationController in conjunction with the first view controller on its stack. Try this:
Also note that you need to release anything you alloc or retain. You can do it as I did by adding
autorelease
when you initialize them or you can release them explicitly after you've added them to thecontrollers
array.You then configure the
navigationItem
for each view controller in itsloadView
orviewDidLoad
method depending on how you implemented it.