gpt4 book ai didi

iphone - xcode 中的 UITabbarController 中没有选择 tabbaritem?

转载 作者:行者123 更新时间:2023-12-03 21:15:51 25 4
gpt4 key购买 nike

我想加载一个未选择 tabBartabBarController。实际上每个 tabBarItem 对应于某个 ViewController。但是我有一个 View “Success”,它不属于任何 tabBarItem。所以当 Successview 出现时,我需要一个 需要出现具有三个 tabBarItems(搜索、设置)的 tabBarController,当选择任何 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 中。我哪里出错了?

最佳答案

即使我遇到同样的情况,我也使用 UITabbar 和选项卡栏项而不是 UITabBarController 因为 UITabBarController 期望来自任何选项卡栏的当前 View 放置的元素...这是代码..这应该可以帮助您:)

 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];

确保在头文件中添加委托(delegate) 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com