这是一种方法:
A.跟踪什么样的用户登录持有一个变量,从你以前的 viewController 传递,或者集中在一个数据对象中:
bool userCanAccessProfile = false;
B.根据上面的布尔值,相应地更新您的布局和逻辑代码:
//layout your tab bar
UITabBar * tabBar = [UITabBar new];
tabBar.frame = CGRectMake(0, h-50, w, 50);
tabBar.delegate = self;
[self.view addSubview:tabBar];
//create the item(s)
UITabBarItem * item = [UITabBarItem new];
item.title = (userCanAccessProfile) ? @"Profile" : @"Settings";
item.image = (userCanAccessProfile) ? [UIImage imageNamed:@"profile.png"] : [UIImage imageNamed:@"settings.png"];
[tabBar setItems:@[item]];
上面的行看起来像这样,意思是:
something = (isThisTrue) ? (true) setThisValue : (false) setAnotherValue;
您是在询问 userCanAccessProfile 是否为真,如果是,则相应地设置不同的文本和图像。
C.当用户单击该项目时,您将再次查询 bool 以了解要执行的操作:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
//when the item is clicked
if (userCanAccessProfile){
//open profile
} else {
//open settings
}
}
请务必在 .m 文件中设置委托:
tabBar.delegate = self;
并在 .h 文件中添加委托:
@interface yourVC : UIViewController <UITabBarDelegate>