我对iOS应用程序开发相对较新。目前我正在开发一个带标签栏的小应用程序。我面临的问题是我希望在foreach选项卡中有不同的导航项。我尝试了很多东西,但事情并没有奏效。我使用原生iOS语言进行编程。
在我的应用程序中,我有一个AppDelegate。在我的AppDelegate中,有一小段代码用于设置我的mainViewController:
- (void)setupOverlordViewController
{
MainViewController *rootVC = [[MainViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
self.window.rootViewController = navVC;
}
我在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选项卡。
将按钮添加到导航栏的代码如下:
UIBarButtonItem *btnNewRecord = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(btnNewRecord)];
NSArray *rightItems = [NSArray arrayWithObjects:btnNewRecord, nil];
[self.navigationItem setRightBarButtonItems:rightItems];
有人可以解释一下我做错了吗?
答案 0 :(得分:3)
我已经使用xib文件为您创建了一个示例。我创建了三个视图控制器并将它们添加到导航控制器。遵循appdelegate
代码:
- (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;
}
以下是输出:
您可以下载代码示例here
答案 1 :(得分:0)
每个标签栏视图控制器都需要单独的导航控制器。然后你可以在每个导航控制器上添加UIBarButtonItem。