通过在 xcode 中单击按钮启用和禁用选项卡栏项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12545034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enable and disable the tab bar item by a button click in xcode?
提问by RonzyFonzy
I have 5 tab bar items. The first one will be the login page. When the user haven't logged on other tab bat items will be disabled, but when the user logged by clicking the navigationItem button all the other 4 tab bat items will be enabled.
我有 5 个标签栏项目。第一个将是登录页面。当用户尚未登录时,其他选项卡 bat 项目将被禁用,但当用户通过单击导航项按钮登录时,所有其他 4 个选项卡 bat 项目将被启用。
I have made searched and found nothing... :(
我已经搜索过但一无所获...... :(
Here's my code:
这是我的代码:
MainTabViewController.h
#import <UIKit/UIKit.h>
@interface MainTabViewController : UITabBarController
@property (retain, nonatomic) IBOutlet UITabBar *MainTabBar;
@end
MainTabViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UITabBarItem *tabBarItem = [[MainTabBar items] objectAtIndex:1];
[tabBarItem setEnabled:FALSE];
}
LoginViewController.h
#import <UIKit/UIKit.h>
@interface LoginViewController : UIViewController
@property (retain, nonatomic) IBOutlet UITextField *CustomerUsername;
@property (retain, nonatomic) IBOutlet UITextField *CustomerPassword;
- (IBAction)ResignKeyboardClicked:(id)sender;
@end
LoginViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIBarButtonItem *btnGo = [[UIBarButtonItem alloc] initWithTitle:@"Login" style:UIBarButtonItemStyleBordered target:self action:@selector(loginAction)];
self.navigationItem.rightBarButtonItem = btnGo;
}
- (void) LoginAction {
AppDelegate *passData = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if ([CustomerUsername.text isEqualToString:@""] || [CustomerPassword.text isEqualToString:@""]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Please Fill all the field" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
return;
}
// i will use a code from connect to DB tutorial
NSString *strURL = [NSString stringWithFormat:@"http://localhost:8888/Staff.php?userName=%@&password=%@",CustomerUsername.text, CustomerPassword.text];
// to execute php code
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
// to receive the returend value
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
if ([strResult isEqualToString:@"1"])
{
//MainTabViewController *main = [[MainTabViewController alloc] initWithNibName:nil bundle:nil];
//UITabBarItem *tabBarItem = [[main.MainTabBar items] objectAtIndex:1];
//[tabBarItem setEnabled:TRUE];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"You are now Logged In" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
return;
}
else
{
// invalid information
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Invalide Information" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
return;
}
}
For now my code only disable the other 4 tab bar items but I do not know the way to enable all the tab bat items when the user is logged in.
现在我的代码只禁用其他 4 个标签栏项目,但我不知道在用户登录时启用所有标签栏项目的方法。
Please help?
请帮忙?
Thanks! :D
谢谢!:D
回答by RonzyFonzy
I have to say that I am a beginner in iOS development bit I think I can help you.
我不得不说,我是 iOS 开发的初学者,我想我可以帮助你。
In you Storyboard make a TabBarController and all the other UIViewController's. Link them to the TabBarController and add assign classes to them. In your case one of the UIViewController will be called LoginViewController.Now when your app starts the LoginViewController must be the first tab an you simply add this code to disable the tabs:
在 Storyboard 中创建一个 TabBarController 和所有其他 UIViewController。将它们链接到 TabBarController 并向它们添加分配类。在您的情况下,其中一个 UIViewController 将被称为 LoginViewController。现在,当您的应用程序启动时,LoginViewController 必须是第一个选项卡,您只需添加此代码即可禁用这些选项卡:
[[[[self.tabBarController tabBar]items]objectAtIndex:1]setEnabled:FALSE];
[[[[self.tabBarController tabBar]items]objectAtIndex:2]setEnabled:FALSE];
[[[[self.tabBarController tabBar]items]objectAtIndex:3]setEnabled:FALSE];
And again you can enable them with:
同样,您可以通过以下方式启用它们:
[[[[self.tabBarController tabBar]items]objectAtIndex:1]setEnabled:TRUE];
[[[[self.tabBarController tabBar]items]objectAtIndex:2]setEnabled:TRUE];
[[[[self.tabBarController tabBar]items]objectAtIndex:3]setEnabled:TRUE];
So your LoginAction function would look like this:
所以你的 LoginAction 函数看起来像这样:
- (void) LoginAction {
AppDelegate *passData = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if ([CustomerUsername.text isEqualToString:@""] || [CustomerPassword.text isEqualToString:@""]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Please Fill all the field" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
return;
}
// i will use a code from connect to DB tutorial
NSString *strURL = [NSString stringWithFormat:@"http://localhost:8888/Staff.php?userName=%@&password=%@",CustomerUsername.text, CustomerPassword.text];
// to execute php code
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
// to receive the returend value
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
if ([strResult isEqualToString:@"1"]) {
//MainTabViewController *main = [[MainTabViewController alloc] initWithNibName:nil bundle:nil];
//UITabBarItem *tabBarItem = [[main.MainTabBar items] objectAtIndex:1];
//[tabBarItem setEnabled:TRUE];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"You are now Logged In" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[[[[self.tabBarController tabBar]items]objectAtIndex:1]setEnabled:TRUE];
[[[[self.tabBarController tabBar]items]objectAtIndex:2]setEnabled:TRUE];
[[[[self.tabBarController tabBar]items]objectAtIndex:3]setEnabled:TRUE];
return;
}
else {
// invalid information
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Invalide Information" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
return;
}
}
I hope it helped :D
我希望它有帮助:D
回答by ChavirA
I updated the solution from @RonzyFonzy to work with N number of tab bar items:
我从@RonzyFonzy 更新了解决方案以使用 N 个标签栏项目:
for (UITabBarItem *tmpTabBarItem in [[self.tabBarController tabBar] items])
[tmpTabBarItem setEnabled:NO];