我想问一下,在我展示一个视图控制器之后是否可以显示一个tabbarcontroller。 我已经看过很多关于tabbarcontroller的教程,但是他们都使用这一行放在了AppDelegate中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[self.window addSubview:tabController.view];
[self.window makeKeyAndVisible];
return YES;}
我想做的是首先显示一个视图控制器:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES; }
并且在显示viewController之后,用户必须单击一个按钮以显示带有TABBARCONTROLLER的另一个视图。如果可能,我该怎么做?
答案 0 :(得分:2)
是的,你可以......在你的第一个控制器中,完成一些工作后再按Tabbar控制器。
示例: -
假设你有通过子类化UITabBarController的TabBarController类。
- (void) doSomeJob
{
if(true)
{
TabBarController *aTabBarController = [[TabBarController alloc] initWithNibName:@"TabBarController" bundle:nil];
[self.navigationController pushViewController:aTabBarController animated:YES];
[aTabBarController release];
}
}
已编辑自定义标签栏:
.h文件
@interface TabBarController : UITabBarController<UITabBarControllerDelegate, UINavigationControllerDelegate>
{
UITabBarController *tabController;
UINavigationController *1NavController;
UINavigationController *2hNavController;
}
@property (nonatomic, retain) UITabBarController *tabController;
@property (nonatomic, retain) UINavigationController *1NavController;
@property (nonatomic, retain) UINavigationController *2hNavController;
.m文件:
- (void)viewDidLoad
{
[super viewDidLoad];
tabController = [[UITabBarController alloc] init];
tabController.delegate = self;
tabController.tabBar.backgroundColor = [UIColor clearColor];
//Add some tabs to the controller...
1ViewController *1ViewController = [[1ViewController alloc] initWithNibName: @"1ViewController" bundle: nil];
1NavController = [[UINavigationController alloc] initWithRootViewController:1ViewController];
1NavController.tabBarItem = [[UITabBarItem1 alloc] init];
2ViewController *aSearchViewController = [[2ViewController alloc] initWithNibName: @"2ViewController" bundle: nil];
2NavController = [[UINavigationController alloc] initWithRootViewController:2ViewController];
2NavController.tabBarItem = [[UITabBarItem2 alloc] init];
tabController.viewControllers = [NSArray arrayWithObjects: 1NavController,2NavController,nil];
[self.view addSubview:tabController.view];
}
您还需要相应地实现Tab栏的委托方法....
答案 1 :(得分:0)
我遇到导航控制器和标签栏视图控制器的情况,但是你需要的是视图控制器,所以我根据你的要求修改我的代码。检查n回复 首先在app delegate .h文件中初始化视图控制器和tabbar控制器 &安培;给予财产也像:
{
IBOutlet FirstViewController * fvc;
IBOutlet UITabBarController *tabBarController;
}
@property (nonatomic, retain) IBOutlet FirstViewController * fvc;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
比在.m文件中将视图控制器设置为视图,因为你想先设置简单的视图控制器,如:
fvc = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:[NSBundle mainBundle]];
[self.window addSubview: fvc.view];
现在在这个视图控制器上你想要跳转到tabbar视图控制器用户的特定事件:
AppDelegate_iPhone *appDelegte_iPhone=(AppDelegate_iPhone*)[[UIApplication sharedApplication]delegate];
[[[appDelegte_iPhone viewController] view] removeFromSuperview];
[[appDelegte_iPhone window]addSubview:[[appDelegte_iPhone tabBarController]view]];
[[appDelegte_iPhone tabBarController]setSelectedIndex:0];
答案 2 :(得分:0)
将UITabBarController保留为窗口的根视图控制器。将您的其他视图控制器显示为模态视图控制器。关闭模态视图控制器以显示标签栏控制器。