ios 以编程方式创建 uiTabBarController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8164356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:25:57  来源:igfitidea点击:

Create uiTabBarController programmatically

iosobjective-cuitabbarcontroller

提问by Mehdi

I want to create a UIViewfor a UITabBarController

我想创建UIView一个UITabBarController

Here is my code for the .hfile :

这是我的.h文件代码:

@interface TE : UIViewController <UITabBarControllerDelegate>{
    UITabBarController *tabBarController;
}
@property (nonatomic,retain) UITabBarController *tabBarController;
@end

The viewDidLoadmethod:

viewDidLoad方法:

UIViewController *testVC = [[T1 alloc] init];
UIViewController *otherVC = [[T2 alloc] init];
NSMutableArray *topLevelControllers = [[NSMutableArray alloc] init];
[topLevelControllers addObject: testVC];
[topLevelControllers addObject: otherVC];
tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate = self;
[tabBarController setViewControllers:topLevelControllers animated:NO];
tabBarController.selectedIndex = 0;
self.view = tabBarController.view;

This creates the tab bar controller, but when I click on a tab bar item, I get an error:

这将创建标签栏控制器,但是当我单击标签栏项目时,出现错误:

Thread1:Program receive signal: SIGABRT

线程1:程序接收信号:SIGABRT

Edit: I solved the problem by downloading and modifying the version of http://www.iphonedevcentral.com/create-uitabbarcontroller/

编辑:我通过下载和修改http://www.iphonedevcentral.com/create-uitabbarcontroller/的版本解决了这个问题

回答by ader

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.

您在上面说您不想在 appDelegate 中创建 tabBarController。为什么不?你还会在哪里创造它?tabBarController 必须是根视图控制器,不能是任何其他视图控制器的子级。

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;

}

回答by bbrame

  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]];
    
  1. 子类 UITabBarController

  2. 覆盖 - (void) loadView 方法并包含以下代码

    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.

差不多就是这样。

回答by Kris Subramanian

@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.

@Mehdi,只需将您的 TE 设为 UITabBarController 而不是 UIViewController,然后在其中包含 TabBarController。使管理您的 TabBarController 变得更加容易。回应其他一些人表示您只能将一个 TabBarController 作为窗口的 rootViewController。事实并非如此。可以在需要二级菜单导航的多个位置实例化 UITabBarController。在 TabBar 中有一个 TabBar 没有意义,但是有一个左侧导航菜单,然后在每个菜单项上有一个 TabBar 是有意义的。

回答by ms83

Trying changing

尝试改变

self.view = tabBarController.view;

self.view = tabBarController.view;

to

[self.view addSubview:tabBarController.view];

[self.view addSubview:tabBarController.view];

See if that helps.

看看这是否有帮助。

Also try placing this in your -(void)loadViewmethod

也试着把它放在你的-(void)loadView方法中

- (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.

您遇到黑屏的原因可能是因为您没有正确初始化 UIView。

回答by Micha? Zygar

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

更改self.view = tabBarController.view;
[self.view addSubview:tabBarController.view];并且它工作正常