本文介绍了以编程方式创建uiTabBarController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为 UITabBarController创建一个
UIView
I want to create a UIView
for a UITabBarController
以下是 .h
文件的代码:
@interface TE : UIViewController <UITabBarControllerDelegate>{
UITabBarController *tabBarController;
}
@property (nonatomic,retain) UITabBarController *tabBarController;
@end
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:
修改:我通过下载和修改
Edit: I solved the problem by downloading and modifying the version of http://www.iphonedevcentral.com/create-uitabbarcontroller/
推荐答案
-
子类UITabBarController
Subclass UITabBarController
覆盖 - (void)loadView方法并包含以下代码
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]];
这就是它。
这篇关于以编程方式创建uiTabBarController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!