我想加载未选择tabBarControllertabBar。实际上每个tabBarItem都对应一个特定的ViewController。但是我有一个“成功”视图,它不属于任何tabBarItem.So,当出现Successview时,我需要一个带有三个tabBarControllertabBarItems(搜索,设置)需要出现,然后选择任何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];
}


但是我没有找到添加到SuccessView的tabbarController。我哪里出错了?

最佳答案

即使我也有相同的情况,我使用了UITabbar和tabbar项而不是UITabBarController,因为UITabBarController希望从放置的任何tabbar项中获得当前视图...这是代码。这应该可以帮助您:)

 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 ...让我知道..如果您愿意,我会在此处发布代码。

关于iphone - 在Xcode的UITabbarController中没有选择tabbaritem?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13946461/

10-13 06:29