Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何以编程方式添加uitabaritem?_Ios_Objective C_Ios7_Uitabbarcontroller_Uitabbaritem - Fatal编程技术网

Ios 如何以编程方式添加uitabaritem?

Ios 如何以编程方式添加uitabaritem?,ios,objective-c,ios7,uitabbarcontroller,uitabbaritem,Ios,Objective C,Ios7,Uitabbarcontroller,Uitabbaritem,在我的应用程序中,我在“Storyborad”中添加了UITabBarController。 但现在我想以编程方式添加uitabaritem。假设有5个按钮,当用户点击该按钮时,调用我的UITabBarController。 “选项卡栏”中总共有5个选项卡。 e、 g: 选项卡名称:项目1、项目2、项目3、项目4、项目5。 按钮名称:项目1、项目2、项目3、项目4、项目5 假设用户单击item1按钮,调用UITabBarcontroller。现在,用户应该不能看到“tabbarcontrolle

在我的应用程序中,我在“Storyborad”中添加了
UITabBarController
。 但现在我想以编程方式添加
uitabaritem
。假设有5个按钮,当用户点击该按钮时,调用我的
UITabBarController
。 “选项卡栏”中总共有5个选项卡。 e、 g:

选项卡名称:项目1、项目2、项目3、项目4、项目5。 按钮名称:项目1、项目2、项目3、项目4、项目5

假设用户单击item1按钮,调用
UITabBarcontroller
。现在,用户应该不能看到“tabbarcontroller”中的item1选项卡,他应该可以看到重置的“tabBaritem”

有人能帮我解决这个问题吗? 如何以编程方式进行

谢谢


Nilesh J

要调用tabBar click事件,您需要执行以下操作:

调用
uitabardelegate
上的
delegate
方法并实现其方法:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    //self.tabBarItem.title = @"Title";
}
获取现有的
tabBarItems

NSMutableArray *tbItems = [NSMutableArray arrayWithArray:[self.tabBar items]];
//Add your new tabBarItem
[tbItems addObject:itemNew];

//Set your tabBar items to the new array
[self.tabBar setItems:tbItems];
Swift 4

if var controllers = tabBarController?.viewControllers {
   let tabItem = UITabBarItem(title: "Item \(controllers.count)", image: nil, selectedImage: nil)
   let vc = UIViewController() // your new view controller
   vc.tabBarItem = tabItem
   controllers.append(vc) // or insert at index up to you
   tabBarController?.setViewControllers(controllers, animated: true)
}

这是用于自定义Tabbar控制器的。在这段代码中,您可以给出选项卡栏按钮的图像,当您单击一个按钮时,该按钮将根据您的图像进行更改

设置
UITabBarControllerDelegate的委托方法

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self creationTabBar];
    [self.window makeKeyAndVisible];
    return YES;
}

-(void)creationTabBar
{
UINavigationController *myNavigationController;

// Create initialized instance of UITabBarController

UITabBarController *tabBarController = [[UITabBarController alloc] init];

tabBarController.view.frame = CGRectMake(0, 0, self.window.bounds.size.width, self.window.bounds.size.height);

// Create initialized instance of NSMutableArray to hold our UINavigationControllers

NSMutableArray *tabs = [[NSMutableArray alloc] init];

World_ViewController *WorldScreen;
Home_ViewController *HomeScreen;
Favourite_ViewController *FavouriteScreen;
Rank_ViewController *RankScreen;
Setting_ViewController *SettingScreen;


UILabel *item1, *item2, *item3, *item4, *item5;

// Create first UIViewController

WorldScreen = [[World_ViewController alloc] initWithNibName:@"World_ViewController" bundle:nil];
HomeScreen = [[Home_ViewController alloc] initWithNibName:@"Home_ViewController" bundle:nil];
FavouriteScreen= [[Favourite_ViewController alloc] initWithNibName:@"Favourite_ViewController" bundle:nil];
RankScreen = [[Rank_ViewController alloc] initWithNibName:@"Rank_ViewController" bundle:nil];
SettingScreen = [[Setting_ViewController alloc] initWithNibName:@"Setting_ViewController" bundle:nil];

myNavigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];

item1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 430, 64, 50)];
item2 = [[UILabel alloc] initWithFrame:CGRectMake(64, 430, 64, 50)];
item3 = [[UILabel alloc] initWithFrame:CGRectMake(128, 430, 64, 50)];
item4 = [[UILabel alloc] initWithFrame:CGRectMake(195, 430, 64, 50)];
item5 = [[UILabel alloc] initWithFrame:CGRectMake(256, 430, 64, 50)];


myNavigationController = [[UINavigationController alloc] initWithRootViewController:HomeScreen];
[myNavigationController setNavigationBarHidden:YES];
[tabs addObject:myNavigationController];

myNavigationController = [[UINavigationController alloc] initWithRootViewController:WorldScreen];
[myNavigationController setNavigationBarHidden:YES];
[tabs addObject:myNavigationController];

myNavigationController = [[UINavigationController alloc] initWithRootViewController:FavouriteScreen];
[myNavigationController setNavigationBarHidden:YES];
[tabs addObject:myNavigationController];

myNavigationController = [[UINavigationController alloc] initWithRootViewController:RankScreen];
[myNavigationController setNavigationBarHidden:YES];
[tabs addObject:myNavigationController];

myNavigationController = [[UINavigationController alloc] initWithRootViewController:SettingScreen];
[myNavigationController setNavigationBarHidden:YES];
[tabs addObject:myNavigationController];


[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"tabbar.png"]];
[tabBarController setViewControllers:tabs];
[tabBarController setDelegate:self];

[item1 setText:@"item1"];
[item1 setBackgroundColor:[UIColor grayColor]];
[tabBarController.view addSubview:item1];
item1.tag = 101;

[item2 setText:@"item2"];
[item2 setBackgroundColor:[UIColor grayColor]];
[tabBarController.view addSubview:item2];
item2.tag = 102;

[item3 setText:@"item3"];
[item3 setBackgroundColor:[UIColor grayColor]];
[tabBarController.view addSubview:item3];
item3.tag = 103;

[item4 setText:@"item4"];
[item4 setBackgroundColor:[UIColor grayColor]];
[tabBarController.view addSubview:item4];
item4.tag = 104;

[item5 setText:@"item5"];
[item5 setBackgroundColor:[UIColor grayColor]];
[tabBarController.view addSubview:item5];
item5.tag = 105;

[self.window setRootViewController:self.viewController];
}
- (void)tabBarController:(UITabBarController *)tabBarControllerref didSelectViewController:(UIViewController *)viewController
{
UILabel *item1 = (UILabel*)[tabBarControllerref.view viewWithTag:101];
UILabel *item2 = (UILabel*)[tabBarControllerref.view viewWithTag:102];
UILabel *item3 = (UILabel*)[tabBarControllerref.view viewWithTag:103];
UILabel *item4 = (UILabel*)[tabBarControllerref.view viewWithTag:104];
UILabel *item5 = (UILabel*)[tabBarControllerref.view viewWithTag:105];

[item1 setFrame: CGRectMake(0, 430, 64, 50)];
[item2 setFrame: CGRectMake(64, 430, 64, 50)];
[item3 setFrame: CGRectMake(128, 430, 64, 50)];
[item4 setFrame: CGRectMake(195, 430, 64, 50)];
[item5 setFrame: CGRectMake(256, 430, 64, 50)];

if([tabBarControllerref selectedIndex] == 0)
{
    [item1 setTextColor:[UIColor redColor]];
    [item2 setTextColor:[UIColor greenColor]];
    [item3 setTextColor:[UIColor greenColor]];
    [item4 setTextColor:[UIColor greenColor]];
    [item5 setTextColor:[UIColor greenColor]];
}
else if ([tabBarControllerref selectedIndex] == 1)
{
    [item1 setTextColor:[UIColor greenColor]];
    [item2 setTextColor:[UIColor redColor]];
    [item3 setTextColor:[UIColor greenColor]];
    [item4 setTextColor:[UIColor greenColor]];
    [item5 setTextColor:[UIColor greenColor]];
}
else if ([tabBarControllerref selectedIndex] == 2)
{
    [item1 setTextColor:[UIColor greenColor]];
    [item2 setTextColor:[UIColor greenColor]];
    [item3 setTextColor:[UIColor redColor]];
    [item4 setTextColor:[UIColor greenColor]];
    [item5 setTextColor:[UIColor greenColor]];
}
else if ([tabBarControllerref selectedIndex] == 3)
{
    [item1 setTextColor:[UIColor greenColor]];
    [item2 setTextColor:[UIColor greenColor]];
    [item3 setTextColor:[UIColor greenColor]];
    [item4 setTextColor:[UIColor redColor]];
    [item5 setTextColor:[UIColor greenColor]];
}
else if ([tabBarControllerref selectedIndex] == 4)
{
    [item1 setTextColor:[UIColor greenColor]];
    [item2 setTextColor:[UIColor greenColor]];
    [item3 setTextColor:[UIColor greenColor]];
    [item4 setTextColor:[UIColor greenColor]];
    [item5 setTextColor:[UIColor redColor]];
}

}

尝试此iOS12时出现新错误,
由于未捕获的异常而终止应用程序“NSInternalInconsistencyException”,原因是:“不允许直接修改由选项卡栏控制器管理的选项卡栏。”
@AlbertRenshaw刚刚为Swift 4添加了一个代码段
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self creationTabBar];
    [self.window makeKeyAndVisible];
    return YES;
}

-(void)creationTabBar
{
UINavigationController *myNavigationController;

// Create initialized instance of UITabBarController

UITabBarController *tabBarController = [[UITabBarController alloc] init];

tabBarController.view.frame = CGRectMake(0, 0, self.window.bounds.size.width, self.window.bounds.size.height);

// Create initialized instance of NSMutableArray to hold our UINavigationControllers

NSMutableArray *tabs = [[NSMutableArray alloc] init];

World_ViewController *WorldScreen;
Home_ViewController *HomeScreen;
Favourite_ViewController *FavouriteScreen;
Rank_ViewController *RankScreen;
Setting_ViewController *SettingScreen;


UILabel *item1, *item2, *item3, *item4, *item5;

// Create first UIViewController

WorldScreen = [[World_ViewController alloc] initWithNibName:@"World_ViewController" bundle:nil];
HomeScreen = [[Home_ViewController alloc] initWithNibName:@"Home_ViewController" bundle:nil];
FavouriteScreen= [[Favourite_ViewController alloc] initWithNibName:@"Favourite_ViewController" bundle:nil];
RankScreen = [[Rank_ViewController alloc] initWithNibName:@"Rank_ViewController" bundle:nil];
SettingScreen = [[Setting_ViewController alloc] initWithNibName:@"Setting_ViewController" bundle:nil];

myNavigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];

item1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 430, 64, 50)];
item2 = [[UILabel alloc] initWithFrame:CGRectMake(64, 430, 64, 50)];
item3 = [[UILabel alloc] initWithFrame:CGRectMake(128, 430, 64, 50)];
item4 = [[UILabel alloc] initWithFrame:CGRectMake(195, 430, 64, 50)];
item5 = [[UILabel alloc] initWithFrame:CGRectMake(256, 430, 64, 50)];


myNavigationController = [[UINavigationController alloc] initWithRootViewController:HomeScreen];
[myNavigationController setNavigationBarHidden:YES];
[tabs addObject:myNavigationController];

myNavigationController = [[UINavigationController alloc] initWithRootViewController:WorldScreen];
[myNavigationController setNavigationBarHidden:YES];
[tabs addObject:myNavigationController];

myNavigationController = [[UINavigationController alloc] initWithRootViewController:FavouriteScreen];
[myNavigationController setNavigationBarHidden:YES];
[tabs addObject:myNavigationController];

myNavigationController = [[UINavigationController alloc] initWithRootViewController:RankScreen];
[myNavigationController setNavigationBarHidden:YES];
[tabs addObject:myNavigationController];

myNavigationController = [[UINavigationController alloc] initWithRootViewController:SettingScreen];
[myNavigationController setNavigationBarHidden:YES];
[tabs addObject:myNavigationController];


[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"tabbar.png"]];
[tabBarController setViewControllers:tabs];
[tabBarController setDelegate:self];

[item1 setText:@"item1"];
[item1 setBackgroundColor:[UIColor grayColor]];
[tabBarController.view addSubview:item1];
item1.tag = 101;

[item2 setText:@"item2"];
[item2 setBackgroundColor:[UIColor grayColor]];
[tabBarController.view addSubview:item2];
item2.tag = 102;

[item3 setText:@"item3"];
[item3 setBackgroundColor:[UIColor grayColor]];
[tabBarController.view addSubview:item3];
item3.tag = 103;

[item4 setText:@"item4"];
[item4 setBackgroundColor:[UIColor grayColor]];
[tabBarController.view addSubview:item4];
item4.tag = 104;

[item5 setText:@"item5"];
[item5 setBackgroundColor:[UIColor grayColor]];
[tabBarController.view addSubview:item5];
item5.tag = 105;

[self.window setRootViewController:self.viewController];
}
- (void)tabBarController:(UITabBarController *)tabBarControllerref didSelectViewController:(UIViewController *)viewController
{
UILabel *item1 = (UILabel*)[tabBarControllerref.view viewWithTag:101];
UILabel *item2 = (UILabel*)[tabBarControllerref.view viewWithTag:102];
UILabel *item3 = (UILabel*)[tabBarControllerref.view viewWithTag:103];
UILabel *item4 = (UILabel*)[tabBarControllerref.view viewWithTag:104];
UILabel *item5 = (UILabel*)[tabBarControllerref.view viewWithTag:105];

[item1 setFrame: CGRectMake(0, 430, 64, 50)];
[item2 setFrame: CGRectMake(64, 430, 64, 50)];
[item3 setFrame: CGRectMake(128, 430, 64, 50)];
[item4 setFrame: CGRectMake(195, 430, 64, 50)];
[item5 setFrame: CGRectMake(256, 430, 64, 50)];

if([tabBarControllerref selectedIndex] == 0)
{
    [item1 setTextColor:[UIColor redColor]];
    [item2 setTextColor:[UIColor greenColor]];
    [item3 setTextColor:[UIColor greenColor]];
    [item4 setTextColor:[UIColor greenColor]];
    [item5 setTextColor:[UIColor greenColor]];
}
else if ([tabBarControllerref selectedIndex] == 1)
{
    [item1 setTextColor:[UIColor greenColor]];
    [item2 setTextColor:[UIColor redColor]];
    [item3 setTextColor:[UIColor greenColor]];
    [item4 setTextColor:[UIColor greenColor]];
    [item5 setTextColor:[UIColor greenColor]];
}
else if ([tabBarControllerref selectedIndex] == 2)
{
    [item1 setTextColor:[UIColor greenColor]];
    [item2 setTextColor:[UIColor greenColor]];
    [item3 setTextColor:[UIColor redColor]];
    [item4 setTextColor:[UIColor greenColor]];
    [item5 setTextColor:[UIColor greenColor]];
}
else if ([tabBarControllerref selectedIndex] == 3)
{
    [item1 setTextColor:[UIColor greenColor]];
    [item2 setTextColor:[UIColor greenColor]];
    [item3 setTextColor:[UIColor greenColor]];
    [item4 setTextColor:[UIColor redColor]];
    [item5 setTextColor:[UIColor greenColor]];
}
else if ([tabBarControllerref selectedIndex] == 4)
{
    [item1 setTextColor:[UIColor greenColor]];
    [item2 setTextColor:[UIColor greenColor]];
    [item3 setTextColor:[UIColor greenColor]];
    [item4 setTextColor:[UIColor greenColor]];
    [item5 setTextColor:[UIColor redColor]];
}

}