基于视图的应用程序中的选项卡栏项目不起作用

发布于 2025-01-02 19:40:10 字数 940 浏览 0 评论 0原文

我通过笔尖创建了一个选项卡栏,其中包含基于视图的应用程序中的三个项目。
我希望在视图出现时默认选择第一个项目。

问题是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薔薇婲 2025-01-09 19:40:10

刚刚搞定..

   -(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
tabBar.delegate = self;
[tabBar setSelectedItem:[tabBar.items objectAtIndex:0]];
[self activateTab:1];
 }

- (void)activateTab:(int)index {
switch (index) {
    case 1:
    //condition
    break;
    case 2:
   //condition
    break;
    default:
        break;
}
 }

  - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(@"didSelectItem: %d", item.tag);
[self activateTab:item.tag];    
  }

Just got it done..

   -(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
tabBar.delegate = self;
[tabBar setSelectedItem:[tabBar.items objectAtIndex:0]];
[self activateTab:1];
 }

- (void)activateTab:(int)index {
switch (index) {
    case 1:
    //condition
    break;
    case 2:
   //condition
    break;
    default:
        break;
}
 }

  - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(@"didSelectItem: %d", item.tag);
[self activateTab:item.tag];    
  }
聽兲甴掵 2025-01-09 19:40:10

看来您需要对 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 of UIViewController, 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文