第二视图中的标签栏
Posted
技术标签:
【中文标题】第二视图中的标签栏【英文标题】:Tabbar in Second View 【发布时间】:2012-12-13 09:37:01 【问题描述】:我的应用中有一个激活页面,每个用户都必须激活该页面。 激活应用后,用户将移动到标签栏视图。
我创建了一个选项卡栏应用程序,从我的activationView 中单击按钮,我试图调用选项卡栏,我得到了整个黑屏。
- (IBAction)moveToActivateView:(id)sender
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
UITabBarController *tabBarController = [[UITabBarController alloc]init];
[self.view addSubview:tabBarController.view];
appDelegate.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
self.tabBarController.viewControllers = @[viewController1, viewController2];
appDelegate.window.rootViewController = self.tabBarController;
[appDelegate.window makeKeyAndVisible];
【问题讨论】:
【参考方案1】:嘿,在 appDelegate 中创建 tabBarController 的属性并在那里分配所有 ViewController,然后从 yourViewController 调用你的 tabBarController
在 AppDelegate.h 中
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (retain, nonatomic) IBOutlet UITabBarController *tabBarController;
在 AppDelegate.m 中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self makeTabBar];
[self addInitialVIew];
[self.window makeKeyAndVisible];
return YES;
-(void)makeTabBar
tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate=self;
FirstViewController *firstVC =[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
UINavigationController *firstNC = [[UINavigationController alloc] initWithRootViewController:firstVC];
firstNC.tabBarItem.title=@"Profile";
firstVC.tabBarController.tabBar.tag = 0;
SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
UINavigationController *SecondNavController = [[UINavigationController alloc] initWithRootViewController:secondVC];
SecondNavController.tabBarItem.title = @"Search";
secondVC.tabBarController.tabBar.tag = 1;
NSArray *viewControllers =[[NSArray alloc]initWithObjects:firstNC,SecondNavController, nil];
[tabBarController setViewControllers:viewControllers animated:NO];
-(void) addInitialVIew
InitialViewController *initialViewController = [[InitialViewController alloc]initWithNibName:@"InitialViewController" bundle:nil];
navigationController = [[UINavigationController alloc]initWithRootViewController:ViewController];
[self.window addSubview:navigationController.view];
现在在InitialViewController
中,您可以添加您的TabBar 并删除InitialViewController
- (IBAction)switchToTabBarBtnPress:(id)sender
AppDelegate *appdelegte =(AppDelegate*)[[UIApplication sharedApplication]delegate];
[[[appdelegte navigationController] view]removeFromSuperview];
[[appdelegte window]addSubview:[[appdelegte tabBarController]view]];
[[appdelegte tabBarController]setSelectedIndex:0];
并按照我的回答
Answer
【讨论】:
【参考方案2】:您想创建动态标签栏应用程序来解决您的问题。因此,您只需创建基于视图的应用程序。在视图控制器 viewdidload 方法中放入这些代码
tabbar1 = [[UITabBarController alloc] init];
artist_tab_obj = [[artist_tab alloc] initWithNibName:@"artist_tab" bundle:nil];
UINavigationController *tabItem1 = [[[UINavigationController alloc] initWithRootViewController: artist_tab_obj] autorelease];
tabItem1.title=@"Artist";
tabItem1.tabBarItem.image=[UIImage imageNamed:@"Icon1.png"];
music_tab_obj = [[music_tab alloc] initWithNibName:@"music_tab" bundle:nil];
UINavigationController *tabItem2 = [[[UINavigationController alloc] initWithRootViewController: music_tab_obj] autorelease];
tabItem2.title=@"Music";
tabItem2.tabBarItem.image=[UIImage imageNamed:@"Icon2.png"];
shout_tab_obj = [[shout_tab alloc] initWithNibName:@"shout_tab" bundle:nil];
UINavigationController *tabItem3 = [[[UINavigationController alloc] initWithRootViewController: shout_tab_obj] autorelease];
tabItem3.title=@"Shout";
tabItem3.tabBarItem.image=[UIImage imageNamed:@"Icon3.png"];
schedule_tab_obj = [[schedule_tab alloc] initWithNibName:@"schedule_tab" bundle:nil];
UINavigationController *tabItem4 = [[[UINavigationController alloc] initWithRootViewController: schedule_tab_obj] autorelease];
tabItem4.title=@"Schedule";
tabItem4.tabBarItem.image=[UIImage imageNamed:@"Icon4.png"];
follow_tab_obj = [[follow_tab alloc] initWithNibName:@"follow_tab" bundle:nil];
UINavigationController *tabItem5 = [[[UINavigationController alloc] initWithRootViewController: follow_tab_obj] autorelease];
tabItem5.title=@"Follower";
tabItem5.tabBarItem.image=[UIImage imageNamed:@"Icon5.png"];
tabbar1.viewControllers = [NSArray arrayWithObjects:tabItem1, tabItem2,tabItem3,tabItem4,tabItem5,nil];
在用户接受是按钮操作时调用此代码。
[self.view insertSubview:tabbar1.view belowSubview: artist_tab_obj.view];
tabbar1.selectedIndex=1;
[self presentModalViewController:tabbar1 animated:YES];
【讨论】:
我已经尝试实现你的代码,我收到一个错误Property 'viewControllers' not found on object of type 'UIViewController *'
请你也分享相应的h文件。【参考方案3】:
您是否意识到您的本地变量tabBarController
并不相同,而是隐藏了属性self.tabBarController
?您创建一个新的UITabBarCotnroller
并将其分配给您的局部变量tabBarController
,该变量只能通过此方法访问。然后你操作(添加新创建的视图控制器)等到self.tabBarController
。 Self.tabBarController
在这里或其他任何地方很容易为零。但这不是您刚刚创建的UITabBarController
。
它是self.tabBarController
(可能为零)你分配给窗口的rootViewController
。
您确实将tabBarController's
视图作为子视图添加到self.view。除此之外,我不知道 self 到底是什么,我认为手动将 tabBar 的视图添加为子视图真的没有必要。设置根视图控制器(正确)应该足够了
【讨论】:
以上是关于第二视图中的标签栏的主要内容,如果未能解决你的问题,请参考以下文章