3

I have tab bar controller with 3 tabs: Search, Messages, My Page

Authorized user can see some information in all of 3 tabs.

But for unauthorized user - when he clicks on messages or my page - I want to show this:

Please sign-in or register [button sign-in] [button registration]

I don't want to show it as modal, I want to keep tab bar at bottom. Clicking on [button sign-in] then reveals modal sign-in screen.

So what is proper approach for this? Do I need somehow create reusable view controller? If so, how do I show it for many tabs in tab bar controller and then switch back to normal view controllers after sign-in?

Roman
  • 3,799
  • 4
  • 30
  • 41
  • when use click on message you can check whether its authorised or not and according to this show information. – Buntylm Jan 23 '14 at 04:41
  • yes but main question is **how** – Roman Jan 23 '14 at 04:42
  • 1
    Create a subclass of `UIView` that contains the information you want to show, then if the user is not authorized add it to the view – Max Chuquimia Jan 23 '14 at 04:42
  • 1
    `- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{ ` use this delegate for `UITabbarController` – Buntylm Jan 23 '14 at 04:43
  • @Jugale I thought about that but isn't it too complicated if I want to use auto-layout feature - I'll need to programmatically set constrains for all elements (labels, buttons, images). – Roman Jan 23 '14 at 04:46

3 Answers3

1

Use UITabBarControllerDelegate Methods

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
  return NO;// if unauthorized
}

  - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
}
Sunny Shah
  • 12,990
  • 9
  • 50
  • 86
  • +1 thanks for new information I didn't know but it's not what I actually need because I _do want_ to select a tab - but change a content of tab for unauthorized user. – Roman Jan 23 '14 at 05:54
1

You could have the view controller for the 'My Page' be an empty View, check the login status on ViewWillLoad (in the My Page VC) then, based on AUTHORIZED/UNAUTHORIZED, you could load one of two XIBs into the VC.

Alternatively, you could modify the tab bar dynamically based on the login status. On load, a Login Now tab will be removed and a 'My Page' tab can be added. Take a look at something similar I wrote:

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

Note: to use the above code, you would need to make your UITabBarController a custom class and add that to the ViewDidLoad

Nathaniel
  • 836
  • 9
  • 19
  • great tip, thanks! One question: I'm using Storyboards (no XIBs) - so how do I convert this line for Storyboards? `LoginVC * myPage = [[LoginVC alloc] init];`? Because it's doesn't work properly - item is added to tab bar but when I click on it - it shows black screen. – Roman Jan 23 '14 at 05:42
  • 1
    found it here http://stackoverflow.com/questions/16134361/how-to-call-a-view-controller-programmatically . Works fine now, many thanks for you answer! – Roman Jan 23 '14 at 06:02
1

For you there are many approach. Once such Simple is this :

  • Add a view to your keyWindow & keep it hidden. This view should contain all your message & button too. Do this in AppDelegate.

Provide this code in

- (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];
  • Implement your methods DismissMe and LoginMe
  • Now On the click on some thing as you said *when he clicks on messages or my page *

Add this code if user is unauthorized.

[[[UIApplication sharedApplication] keyWindow] bringSubviewToFront:[[[UIApplication sharedApplication] keyWindow] viewWithTag:666666]];
[[[[UIApplication sharedApplication] keyWindow] viewWithTag:666666] setAlpha:1.0];
  • Now in LoginMe implementation, you can do something like this 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];
    }
    

here is some screenshot: - My home Screen

Home Screen

  • SignIn Cancel if Unauthorised.

SignIn Cancel If Unauthorised.

I hope that helps.

Balram Tiwari
  • 5,657
  • 2
  • 23
  • 41