简体   繁体   English

在IOS的Cocoa Touch Tab Bar Application前面添加登录屏幕

[英]Adding login screen in front of Cocoa Touch Tab Bar Application for IOS

Still getting my head around things here. 仍然在这里绕开我的脑袋。 I'm not even close, but anyways.... I have a TabBar application created from Xcode. 我什至没有关闭,但是无论如何...。我有一个从Xcode创建的TabBar应用程序。 It works I have three tab views, that I know how to manipulate, etc. 它的工作原理是我有三个选项卡视图,知道如何操作等。

I'd like to put a 'login' nib file in front of this whole thing, requiring a user to answer a (hardcoded for now) username and password. 我想在整个过程前放置一个“ login” nib文件,要求用户回答一个(目前已硬编码)用户名和密码。 If you get that right, then, render the tab portion, allowing them to click around. 如果正确,则渲染选项卡部分,允许他们单击。

I have another application that I've written that does the username and password part, I'm having trouble taking the logic from there, and putting it in front of the TabApplication piece. 我已经编写了另一个应用程序,该应用程序负责用户名和密码部分,我无法从那里获取逻辑并将其放在TabApplication部分的前面。

Anyone have any suggestions? 有人有什么建议吗?

In your AppDelegate, at the end of the application didFinishLaunchingWithOptions method you'll see this: 在您的AppDelegate中,在application didFinishLaunchingWithOptions方法末尾,您将看到以下内容:

[window addSubview:tabcontroller.view];
[window makeKeyAndVisible];
return YES;

Simply initialize your login view controller and add it after the tabcontroller, like this: 只需初始化您的登录视图控制器并将其添加到tabcontroller之后,如下所示:

initialScreenViewController = [[InitialScreenViewController alloc] init];
[window addSubview:tabcontroller.view];
[window addSubview:initialScreenViewController.view];
[window makeKeyAndVisible];
return YES;

In you login viewcontroller, after authenticating the user you can hide it like this: 在登录viewcontroller中,对用户进行身份验证后,可以像这样隐藏它:

[self.parentViewController.view setHidden:YES];

which allows you to show it again if you have a logout feature. 如果您具有退出功能,则可以再次显示它。

The standard way is the following: 标准方法如下:

  • Package everything related to login screen into a view and a UIViewController subclass which manages that. 将与登录屏幕相关的所有内容打包到一个视图和一个用于管理该视图的UIViewController子类中。
  • Present that view modally in the app delegate in application:didFinishLaunchingWithOptions: by calling 通过调用在application:didFinishLaunchingWithOptions:中的应用程序委托中以模态形式显示该视图

     LoginController*loginController= ... ; // create the view controller for the login screen [self.tabController presentModalViewController:loginController animated:YES]; 

This way, the animation between the transition etc. is automatically handled. 这样,将自动处理过渡等之间的动画。

You can later dismiss it after you successfully logs in. It can be done from inside the LoginController by 您可以稍后在成功登录后将其关闭。可以通过在LoginController内部执行以下操作:

[self.parentViewController dismissModalViewControllerAnimated:YES];

However, I often need to do additional setup once the logging-in is done. 但是,登录完成后,我经常需要进行其他设置。 So, I would first tell the app delegate that the login is done, and then perform 因此,我首先要告诉应用程序代表登录已完成,然后执行

[self.tabController dismissModalViewControllerAnimated:YES];

from the app delegate. 来自应用程序委托。 Then I can perform additional tasks there. 然后,我可以在那里执行其他任务。

To communicate back to the app delegate, I would use NSNotification , but that might be a bit difficult for you. 为了与应用程序NSNotification通信,我将使用NSNotification ,但这对您来说可能有点困难。

One way which might be easier to understand (but uglier to my taste) is to define a method, say loginDone in the app delegate. 一种可能更容易理解(但loginDone我口味)的方法是定义一个方法,例如在应用程序委托中使用loginDone Then, inside the LoginViewController , you can do 然后,在LoginViewController ,您可以执行

MyAppDelegate*appDelegate=[[UIApplication sharedApplication] delegate];
[appDelegate loginDone];

If you are starting with the default tab bar application you can do it like this: 如果从默认选项卡栏应用程序开始,则可以这样操作:

  • In the MainWindow.xib, create a UIView that contains all the stuff you want to have on your password screen 在MainWindow.xib中,创建一个UIView,其中包含您希望在密码屏幕上拥有的所有内容
  • Hook whatever you need up to IBOutlets in the AppDelegate, and write the method that checks if the password is valid. 在AppDelegate中将所需的任何内容挂接到IBOutlets,然后编写检查密码是否有效的方法。
  • In the applicationDidFinishLaunching method, replace [window addSubview:tabBarController.view]; 在applicationDidFinishLaunching方法中,替换[window addSubview:tabBarController.view]; with [window addSubview:/*whatever you called the view with the password stuff in it*/]; [window addSubview:/*whatever you called the view with the password stuff in it*/];
  • If the user enters the correct password do this: 如果用户输入正确的密码,请执行以下操作:

[passView removeFromSuperview]; [window addSubview:tabBarController.view];

And you should be in the regular tab bar application. 并且您应该在常规的标签栏应用程序中。

I prefer to do the following: 我更喜欢执行以下操作:

In App Delegate's didFinishLaunchingWithOptions : 在App Delegate的didFinishLaunchingWithOptions

FirstViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
ThirdViewController *viewController3 = [[ThirdViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
UINavigationController *navController3 = [[UINavigationController alloc] initWithRootViewController:viewController3];

self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[navController1, navController2, navController3];

LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
UINavigationController *loginNavController = [[UINavigationController alloc] initWithRootViewController:loginViewController];

self.window.rootViewController = loginNavController;

Then after getting an authentication callback, you can have something like this in your App Delegate: 然后,在获得身份验证回调后,您可以在App Delegate中添加以下内容:

- (void)setAuthenticatedState:(BOOL)authenticated
{
    if (authenticated == YES) {
        dispatch_async(dispatch_get_main_queue(), ^(){
            self.window.rootViewController = self.tabBarController;
        });
    }else{
        // Stuff
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM