第二个视图中的Tabbar

时间:2012-12-13 09:37:01

标签: iphone objective-c ios uitabbar

我的应用中有一个激活页面,每个用户都必须激活该应用。 激活应用程序后,用户将移至选项卡栏视图。

我创建了一个标签栏应用程序,从我的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];}

3 个答案:

答案 0 :(得分:2)

嘿,在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,您可以添加yourTabBar并移除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

答案 1 :(得分:0)

您是否意识到您的本地变量tabBarController不完全相同但隐藏属性self.tabBarController?您创建一个新的UITabBarCotnroller并将其分配给您的本地变量tabBarController,该变量只能从此方法访问。然后你操纵(添加新创建的视图控制器)等self.tabBarControllerSelf.tabBarController在这里或其他任何地方都很容易。但它不是你刚刚创建的UITabBarController

self.tabBarController(可能为零)你指定给rootViewController的窗口。

您确实将tabBarController's视图作为子视图添加到self.view。除此之外,我不知道自己的实际情况是什么,我认为手动添加tabBar的视图作为子视图是不是真的很有必要。设置根视图控制器(正确)应该足够了

答案 2 :(得分:0)

您想要创建动态标签栏应用程序来解决您的问题。所以你只需创建基于视图的应用程序。在视图控制器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];