开发者

Using a root view controller to determine which view to load

开发者 https://www.devze.com 2023-04-04 05:03 出处:网络
I ha开发者_如何学JAVAve come across a situation today which has me wondering about best practices. I would greatly appreciate any comments on how you would approach this simplified example:

I ha开发者_如何学JAVAve come across a situation today which has me wondering about best practices. I would greatly appreciate any comments on how you would approach this simplified example:

Let's say we have an app that requires a login to be useful. So, we have a couple of views and corresponding view controllers: LoginView and MainView. MainView is the root view and root controller for a navigation controller. LoginView is a view which allows the user to login.

So, the first time the app is launched, LoginView should be displayed, then MainView once the login is completed. On subsequent launches, only MainView will be displayed.

One approach to this would be to handle all of this in applicationDidFinishLaunching:

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

    UIViewController *rootController = [[MainView alloc] init];
    navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible];

    if ( notLoggedIn ) {
        LoginView *vc = [[LoginView alloc] initWithNibName:@"LoginView" bundle:nil];
        [rootController presentModalViewController:vc animated:NO];
    }

    return YES;
}

It would be nice to be able to handle this in a separate, dedicated "root" view controller. This controller would be loaded by the AppDelegate, and it would in turn load whichever view controller is appropriate. Can anyone offer advice on if this would be a better approach? And if so, how to go about it?

Is there a different approach you would recommend in a situation like this?

Thanks, all.


One approach would be to have a authentication provider which is a delegate. In your model classes which your views consumes, you can set the authentication provider. The delegate is a protocol which has a signature to authenticate and determine if authenticated.

The gui would provide the model with an authentication provider delegate which can answer whether authenticated and if not would present a modal view controller to authenticate. Different model methods would ensure authenticated (by asking the provider) and if not, would call authenticate on the providers delegate. Since the gui sets the auth provider, the model isn't breaking encapsulation and baking in gui interactions. the model is simply calling a callback.

That means it doesn't matter which view you're on and what state you're in. As any particular view crosses the model, if you're not authenticated you will get prompted. As another example, let's say the auth token times out after a period of time. How do you do that if auth is baked into one specific view on startup?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号