0

我的应用程序中有一个激活页面,每个用户都必须激活该应用程序。一旦应用程序被激活,用户就会移动到标签栏视图。

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

3 回答 3

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];
}

并按照我的回答

回答

于 2012-12-13T09:46:19.700 回答
0

您是否意识到您的本地变量tabBarController并不相同,而是隐藏了属性self.tabBarController?您创建一个新UITabBarCotnroller变量并将其分配给您的局部变量tabBarController,该变量只能通过此方法访问。然后你操作(添加新创建的视图控制器)等到self.tabBarController. Self.tabBarController在这里很容易为零或其他任何东西。但它不是UITabBarController你刚刚创建的那个。

它是self.tabBarController(很可能为零)您分配给窗口的rootViewController.

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

于 2012-12-13T09:47:22.417 回答
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]; 
于 2012-12-13T10:02:06.033 回答