我写的这个应用程序有一个问题。
我正在设置 UITabBar
在我的应用程序窗口中,并在视图文件中设置图标。
但是,当我运行应用程序时,第一个图标显示(因为视图已加载,我猜),其他图标在我点击它们之前不会显示。
我需要实施吗? self.tabBarItem
在其他一些方法中没有 viewDidLoad
?
在此先感谢大家!
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
tabBar = [[UITabBarController alloc] init];
SubscriptionsController *subscriptionsController = [[SubscriptionsController alloc] init];
FavoritesController *favoritesController = [[FavoritesController alloc] init];
CategoriesController *categoriesController = [[CategoriesController alloc] init];
TagsController *tagsController = [[TagsController alloc] init];
HelpScreenController *helpScreenController = [[HelpScreenController alloc] init];
tabBar.viewControllers = [NSArray arrayWithObjects:
subscriptionsController,
favoritesController,
categoriesController,
tagsController,
helpScreenController,
nil
];
[window addSubview:tabBar.view];
// Override point for customization after application launch.
[window makeKeyAndVisible];
return YES;
}
//The View
- (void)viewDidLoad {
[super viewDidLoad];
tabIcon = [[UITabBarItem alloc] initWithTitle:@"Abonime" image:[UIImage imageNamed:@"subscr.png"] tag:0];
self.tabBarItem = tabIcon;
[tabIcon release];
}
我认为你应该在视图控制器的指定初始化器中设置tabBarItem属性(从你的代码判断,它必须是 -init
对于每个控制器)。事实上,标签栏控制器足够智能,可以按需加载视图,也就是说,应该先设置tabBarItem属性 viewDidLoad
被发送。
此外,您似乎泄漏了所有视图控制器。要解决此问题,请执行以下操作:
SubscriptionsController *subscriptionsController = [[[SubscriptionsController alloc] init] autorelease];
我认为你应该在视图控制器的指定初始化器中设置tabBarItem属性(从你的代码判断,它必须是 -init
对于每个控制器)。事实上,标签栏控制器足够智能,可以按需加载视图,也就是说,应该先设置tabBarItem属性 viewDidLoad
被发送。
此外,您似乎泄漏了所有视图控制器。要解决此问题,请执行以下操作:
SubscriptionsController *subscriptionsController = [[[SubscriptionsController alloc] init] autorelease];
正确。图标不会显示,因为视图(除第一个之外,尚未加载)。并且在您点击视图之前不会加载,因为直到那时才调用viewDidLoad。
删除单个UIViewControllers viewDidLoad中的代码并执行此操作...
NSArray *controllers = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"SubscriptionsController", @"class", [UIImage imageNamed:@"btn_tax.png"], @"icon", @"Abonime", @"title", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"FavoritesController", @"class", [UIImage imageNamed:@"btn_tax.png"], @"icon", @"Abonime", @"title", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"CategoriesController", @"class", [UIImage imageNamed:@"btn_tax.png"], @"icon", @"Abonime", @"title", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"TagsController", @"class", [UIImage imageNamed:@"btn_tax.png"], @"icon", @"Abonime", @"title", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"HelpScreenController", @"class", [UIImage imageNamed:@"btn_tax.png"], @"icon", @"Abonime", @"title", nil],
nil];
NSMutableArray *controllerArray = [NSMutableArray array] ;
for (NSUInteger i = 0; i < [controllers count]; i++)
{
id newClass = [[NSClassFromString([[controllers objectAtIndex:i] objectForKey:@"class"]) alloc] init];
UITabBarItem *tabItem = [[UITabBarItem alloc] init];
tabItem.image = [[controllers objectAtIndex:i] objectForKey:@"icon"];
tabItem.title = [[controllers objectAtIndex:i] objectForKey:@"title"];
tabItem.tag = i;
[(UIViewController*)newClass setTabBarItem:tabItem];
[tabItem release];
[controllerArray addObject:newClass];
[newClass release];
}
tabBar.viewControllers = controllerArray;