基于视图的应用程序中的选项卡栏项目不起作用
我通过笔尖创建了一个选项卡栏,其中包含基于视图的应用程序中的三个项目。
我希望在视图出现时默认选择第一个项目。
问题是 item1 显示已选择,但它没有加载它有权执行的视图。当我们单击该项目时,就会出现视图。请帮我解决这个问题。这是我的代码...
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
tabBar.delegate = self;
[tabBar setSelectedItem:[tabBar.items objectAtIndex:0]];
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(@"didSelectItem: %d", item.tag);
if (item.tag==1) {
ImagesOverlay=[[UIView alloc]initWithFrame:CGRectMake(0, 210, 320, 250)];
ImagesOverlay.backgroundColor=[UIColor grayColor];
[self.view addSubview:ImagesOverlay];
}else if (item.tag==2) {
relatedOverlay=[[UIView alloc]initWithFrame:CGRectMake(0, 210, 320, 250)];
relatedOverlay.backgroundColor=[UIColor redColor];
[self.view addSubview:relatedOverlay];
}else if(item.tag==3){
//other condition
}
}
I have created a tabbar through nib with three items in a view based application.
I want first item get selected by default when the view appear.
The problem is item1 show selected but it doesnt load the view it is entitled to do. when we click on the item the view appears. Please help me to sort out this. here Is my code...
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
tabBar.delegate = self;
[tabBar setSelectedItem:[tabBar.items objectAtIndex:0]];
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(@"didSelectItem: %d", item.tag);
if (item.tag==1) {
ImagesOverlay=[[UIView alloc]initWithFrame:CGRectMake(0, 210, 320, 250)];
ImagesOverlay.backgroundColor=[UIColor grayColor];
[self.view addSubview:ImagesOverlay];
}else if (item.tag==2) {
relatedOverlay=[[UIView alloc]initWithFrame:CGRectMake(0, 210, 320, 250)];
relatedOverlay.backgroundColor=[UIColor redColor];
[self.view addSubview:relatedOverlay];
}else if(item.tag==3){
//other condition
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
刚刚搞定..
Just got it done..
看来您需要对 UITabBarController 的工作原理进行更多研究。您应该向其传递 UIViewController 的实例,而不是手动更改视图。阅读类参考:
https: //developer.apple.com/library/ios/#documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html
It looks like you need to do some more research into how
UITabBarController
works. You should be passing it instances ofUIViewController
, rather than manually changing the views. Have a read of the class reference:https://developer.apple.com/library/ios/#documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html