2

我想加载一个tabBarController没有tabBar选择的。实际上每个都tabBarItem对应一个特定的 ViewController。但是我有一个视图“Success”,它不属于任何tabBarItem。所以当Successview出现时,我需要一个tabBarController带有三个tabBarItems(搜索,设置)的需要出现,然后选择任何tabbaritem然后对应ViewController 应该出现并且 SuccesView 应该消失。通过谷歌找到了这一点,但无法使其正常工作。

在 SuccessView.m 中

- (void)viewDidLoad
{
    [super viewDidLoad];

// UIlabels and UITextFields loads

SuccessView *defaultView = [[SuccessView alloc]initWithNibName:@"SuccessView" bundle:[NSBundle mainBundle]];
    [self.tabBarController setSelectedViewController:nil];
    [self.tabBarController setSelectedViewController:defaultView];

    SearchView *first = [[SearchView alloc] initWithNibName:@"SearchView" bundle:nil];
    first.title=@"Search";

    Settings *second=[[Settings alloc]initWithNibName:@"Settings" bundle:nil];
    second.title=@"Settings";

    NSArray *viewArray= [NSArray arrayWithObjects:first,second, nil];

    tabbarController=[[UITabBarController alloc] init];
    [tabbarController setViewControllers:viewArray animated:NO];

    [self presentModalViewController:tabbarController animated:NO];
}

但我没有发现任何 tabbarController 添加到 SuccessView。我哪里出错了?

4

1 回答 1

1

即使我有相同的情况,我使用UITabbar和标签栏项目,而不是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使用图像自定义...请让我知道...如果您愿意,我会在此处发布代码。

于 2012-12-19T06:26:00.870 回答