我的标签栏控制器有3个标签:搜索,消息,我的页面
授权用户可以在所有3个标签中看到一些信息。
但对于未经授权的用户 - 当他点击消息或我的页面时 - 我想要显示:
请登录或注册[按钮登录] [按钮注册]
我不想将其显示为模态,我想将标签栏保持在底部。点击[button sign-in]
,然后显示模态登录屏幕。
那么适当的方法是什么?我是否需要以某种方式创建可重用的视图控制器?如果是这样,如何在标签栏控制器中显示许多选项卡,然后在登录后切换回普通视图控制器?
答案 0 :(得分:1)
使用 UITabBarControllerDelegate 方法
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
return NO;// if unauthorized
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
}
答案 1 :(得分:1)
您可以让“我的页面”的视图控制器为空视图,检查ViewWillLoad(在我的页面VC中)的登录状态,然后根据AUTHORIZED / UNAUTHORIZED,您可以将两个XIB中的一个加载到VC。
或者,您可以根据登录状态动态修改选项卡栏。在加载时,将删除“立即登录”选项卡,并可添加“我的页面”选项卡。看看我写的类似的东西:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if([prefs boolForKey:@"is_logged_in"]) {
NSMutableArray * vcs = [NSMutableArray
arrayWithArray:self.viewControllers];
[vcs removeObjectAtIndex:2];
LoginVC * myPage = [[LoginVC alloc] init];
[vcs addObject:myPage];
[self setViewControllers:vcs animated:YES];
}
注意:要使用上面的代码,您需要将UITabBarController设为自定义类并将其添加到ViewDidLoad
答案 2 :(得分:1)
对你来说有很多方法。一旦这样简单就是这样:
在
中提供此代码- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
UIView *view = [[UIView alloc]initWithFrame:self.window.frame];
[view setBackgroundColor:[UIColor colorWithWhite:0.2 alpha:0.7]];
UIButton * loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
[loginButton setTitle:@"Sign In" forState:UIControlStateNormal];
[loginButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[loginButton setBackgroundImage:GETIMAGE(@"loginNormal", @"png") forState:UIControlStateNormal];
[loginButton setBackgroundImage:GETIMAGE(@"loginPressed", @"png") forState:UIControlStateHighlighted];
[loginButton setFrame:CGRectMake(100, 300, 100, 40)];
[loginButton addTarget:self action:@selector(LoginMe:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:loginButton];
UIButton * cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
[cancelButton setBackgroundImage:GETIMAGE(@"loginNormal", @"png") forState:UIControlStateNormal];
[cancelButton setBackgroundImage:GETIMAGE(@"loginPressed", @"png") forState:UIControlStateHighlighted];
[cancelButton setFrame:CGRectMake(100, 350, 100, 40)];
[cancelButton addTarget:self action:@selector(DismissMe:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:cancelButton];
[[[UIApplication sharedApplication] keyWindow] addSubview:view];
view.center = [[UIApplication sharedApplication] keyWindow].center;
[view setTag:666666];
[view setAlpha:0.0];
DismissMe
和LoginMe
如果用户未经授权,请添加此代码。
[[[UIApplication sharedApplication] keyWindow] bringSubviewToFront:[[[UIApplication sharedApplication] keyWindow] viewWithTag:666666]];
[[[[UIApplication sharedApplication] keyWindow] viewWithTag:666666] setAlpha:1.0];
现在在LoginMe实现中,您可以执行类似此Modal的操作。
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
if([self.window.rootViewController isKindOfClass:[UITabBarController class]]) {
self.loginController = [[MyLoginViewController alloc]init];
[topController presentViewController:self.loginController animated:YES completion:nil];
}
这是一些截图: - 我的主屏幕
我希望有所帮助。