Create uiTabBarController programmatically

前端 未结 5 2366
隐瞒了意图╮
隐瞒了意图╮ 2021-02-14 09:06

I want to create a UIView for a UITabBarController

Here is my code for the .h file :

@interface TE : UIViewContro         


        
相关标签:
5条回答
  • 2021-02-14 09:34

    You say above that you don't want to create the tabBarController in the appDelegate. Why not? Where else would you create it? The tabBarController has to be the root view controller and cannot be a child of any other view controller.

    Btw, make sure you implement:

    - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    
        NSUInteger tabIndex = [tabBarController.viewControllers indexOfObject:viewController];
    
        if (viewController == [tabBarController.viewControllers objectAtIndex:tabIndex] ) {
             return YES;
        }
    
        return NO;
    
    }
    
    0 讨论(0)
  • 2021-02-14 09:36

    Change self.view = tabBarController.view; to
    [self.view addSubview:tabBarController.view]; And it works correctly

    0 讨论(0)
  • 2021-02-14 09:46

    @Mehdi, just make your TE a UITabBarController instead of a UIViewController which then has a TabBarController in it. Makes it all the more easy to manage your TabBarController. To respond to some others who have indicated that you can have only one TabBarController as the window's rootViewController. That is not the case. A UITabBarController can be instantiated in multiple places where you need a second level menu navigation. Have a TabBar within a TabBar would not make sense, but having a left Navigation Menu and then having a TabBar on each menu item would make sense.

    0 讨论(0)
  • 2021-02-14 09:56

    Trying changing

    self.view = tabBarController.view;

    to

    [self.view addSubview:tabBarController.view];

    See if that helps.

    Also try placing this in your -(void)loadView method

    - (void)loadView {
    
        UIView *mv = [[UIView alloc] initWithFrame:CGRectMake(0.0, 100.0, 320.0, 480.0)];
    
        self.view = mv;
    
        [mv release];
    }
    

    The reason you probably are experiencing a black screen is because you haven't initialized your UIView properly.

    0 讨论(0)
  • 2021-02-14 09:59
    1. Subclass UITabBarController

    2. Override the - (void) loadView method and include the following code

      MyCustomViewControllerOne* ctrl1 = [[[MyCustomViewControllerOne alloc] initWithNibName@"MyViewControllerOne" bundle: nil] autorelease];
      UIViewController* ctrl2 = [[[UIViewController alloc] init] autorelease];
      MyCustomControllerTwo* ctrl3 = [[[UIViewController alloc] initWithObject: myObj] autorelease];
      
      ctrl1.title = @"First tab";
      ctrl2.title = @"Second tab";
      ctrl3.title = @"Third tab";
      
      ctrl1.tabBarItem.image = [UIImage imageNamed:@"tab_image1.png"];
      ctrl2.tabBarItem.image = [UIImage imageNamed:@"tab_image2.png"];
      ctrl3.tabBarItem.image = [UIImage imageNamed:@"tab_image3.png"];
      
      [self setViewControllers: @[ctrl1, ctrl2, ctrl3]];
      

    That's pretty much it.

    0 讨论(0)
提交回复
热议问题