iOS 7:标签栏控制器的不同导航项 [英] iOS 7: different navigation items for tab bar controller
问题描述
我对iOS应用程序开发相对较新。目前我正在开发一个带标签栏的小应用程序。我面临的问题是我希望在foreach选项卡中有不同的导航项。我尝试了很多东西,但事情并没有奏效。我用原生iOS语言编程。
I am relative new with the iOS app development. Currently I am developing a small app with a tab bar. The problem I am facing is that I would like to have different navigation items foreach tab. I tried a lot of things, but things aren't working. I am programming in the native iOS language.
在我的应用程序中,我有一个AppDelegate。在我的AppDelegate中有一小段代码用于设置我的mainViewController:
In my app I've got a AppDelegate. In my AppDelegate there is a little piece of code for setting up my mainViewController:
- (void)setupOverlordViewController
{
MainViewController *rootVC = [[MainViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
self.window.rootViewController = navVC;
}
我在MainViewController中设置我的标签:
I am setting up my tabs in my MainViewController:
- (void)viewDidLoad
{
UIViewController *tabView1 = [[Tab1ViewController alloc] init];
UIViewController *tabView2 = [[Tab2ViewController alloc] init];
UIViewController *tabView3 = [[Tab3ViewController alloc] init];
NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
[tabViewControllers addObject:tabView1];
[tabViewControllers addObject:tabView2];
[tabViewControllers addObject:tabView3];
[self setViewControllers:tabViewControllers];
tabView1.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"TabView1", nil)
image:[UIImage imageNamed:@"tabView1.png"]
tag:1];
tabView2.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"TabView2", nil)
image:[UIImage imageNamed:@"tabView2.png"]
tag:2];
tabView3.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"TabView3", nil)
image:[UIImage imageNamed:@"tabView3.png"]
tag:3];
}
每个视图(tabView1,tabView2,tabView3)都有自己的布局,这是在View的ViewDidLoad方法中设置。当我想通过在ViewDidLoad方法中添加导航按钮时在导航栏中添加导航按钮,但似乎无法添加按钮。添加它们的唯一方法是直接在我的MainViewController中,但是我无法将导航栏按钮设置为不同的foreach选项卡。
Each View (tabView1, tabView2, tabView3) has there own layout, which is set in the ViewDidLoad method of the View. When I would like to add navigation buttons in the navigation bar by adding them in the ViewDidLoad method, but it seems impossible to add the buttons. The only way to add them is directly in my MainViewController, but then I can't set the navigation bar buttons different foreach tab.
将按钮添加到我的代码导航栏如下:
The code for adding the buttons to my navigation bar is as follows:
UIBarButtonItem *btnNewRecord = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(btnNewRecord)];
NSArray *rightItems = [NSArray arrayWithObjects:btnNewRecord, nil];
[self.navigationItem setRightBarButtonItems:rightItems];
有人可以解释一下我做错了吗?
Could somebody explain me what I am doing wrong?
推荐答案
我已经使用xib文件为您创建了一个示例。我创建了三个视图控制器并将它们添加到导航控制器。在 appdelegate
代码之后:
I have created a example for you by using xib files. I have created three View Controllers and added them to navigation controllers. Following the appdelegate
code :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
FirstViewController *firstVC = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil];
UINavigationController *firstNavVC = [[UINavigationController alloc] initWithRootViewController: firstVC];
SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
UINavigationController *secondNavVC = [[UINavigationController alloc] initWithRootViewController: secondVC];
ThirdViewController *thirdVC = [[ThirdViewController alloc] initWithNibName:@"ThirdView" bundle:nil];
UINavigationController *thirdNavVC = [[UINavigationController alloc] initWithRootViewController: thirdVC];
NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
[tabViewControllers addObject:firstNavVC];
[tabViewControllers addObject:secondNavVC];
[tabViewControllers addObject:thirdNavVC];
firstNavVC.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"First", nil)
image:nil
tag:1];
secondNavVC.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"Second", nil)
image:nil
tag:2];
thirdNavVC.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"Third", nil)
image:nil
tag:3];
UITabBarController *tabbarController = [[UITabBarController alloc] init];
tabbarController.viewControllers = tabViewControllers;
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = tabbarController;
[self.window makeKeyAndVisible];
return YES;
}
以下是输出:
您可以下载代码示例这里
这篇关于iOS 7:标签栏控制器的不同导航项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!