在xcode的UITabbarController中没有选择tabbaritem?

时间:2012-12-19 06:06:02

标签: iphone xcode uitabbarcontroller

我想加载tabBarController而未选择tabBar。实际上每个tabBarItem对应一个ViewController。但我有一个视图“成功”,它不属于任何tabBarItem.So当Successview出现时,我需要tabBarController三个{{}需要出现1}}(搜索,设置),然后当选择任何tabbaritem时,应出现相应的ViewController,SuccesView应该消失。 通过谷歌了解并发现这一点,但无法使其正常工作。

在SuccessView.m

tabBarItems

但我没有找到任何tabbarController添加到SuccessView.Where我出错了?

1 个答案:

答案 0 :(得分:1)

即使我使用UITabbar和tabbar项而不是UITabBarController也有相同的情况,因为UITabBarController需要放置任何标签栏项目的当前视图...这是代码。这应该让你去:)

 UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(your frame)];   
  NSMutableArray  *tabBarItemsArray= [[NSMutableArray alloc] init];
    UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:@"artist" image:[UIImage imageNamed:@"artist-tab.png"] tag:1];//assigning the title name and image

    [tabBarItem1 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:48.0/255.0  green:60.0/255.0 blue:109.0/255.0 alpha:1.0],UITextAttributeTextColor,nil] forState:UIControlStateNormal];//set the color when the tabbar appears

    [tabBarItem1 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil] forState:UIControlStateSelected];//sets the color when the tabbar is selected

     UITabBarItem *tabBarItem2 = [[UITabBarItem alloc] initWithTitle:@"Facebook" image:[UIImage imageNamed:@"music-tab.png"] tag:2];

    [tabBarItem2 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:48.0/255.0  green:60.0/255.0 blue:109.0/255.0 alpha:1.0],UITextAttributeTextColor,nil] forState:UIControlStateNormal];

    [tabBarItem2 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil] forState:UIControlStateSelected];

            [tabBarItemsArray addObject:tabBarItem1];
            [tabBarItemsArray addObject:tabBarItem2];
            tabBar.items = tabBarItemsArray;
            tabBar.delegate = self;

         [self.view addSubview:tabBar];

确保在头文件中添加委托UITabBarDelegate以实现此方法

   - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
        NSLog(@"Tabbar selected itm %d",item.tag); 
   // here you can call your view based on tab click you can try using switch statements here based on item.tag
       }

希望这能回答你的问题并提供帮助:)

此外,我们可以使用图片自定义UITabbar ...请告诉我..如果您愿意,我会在此处发布代码。