xcode 在 IOS 的 Cocoa Touch Tab Bar 应用程序前面添加登录屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4406426/
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
Adding login screen in front of Cocoa Touch Tab Bar Application for IOS
提问by John Batdorf
Still getting my head around things here. I'm not even close, but anyways.... I have a TabBar application created from Xcode. It works I have three tab views, that I know how to manipulate, etc.
仍然让我的头脑围绕这里的事情。我什至还没有接近,但无论如何......我有一个从 Xcode 创建的 TabBar 应用程序。我有三个选项卡视图,我知道如何操作,等等。
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. If you get that right, then, render the tab portion, allowing them to click around.
我想在这整个事情前面放一个“登录”nib 文件,要求用户回答(现在是硬编码的)用户名和密码。如果你做对了,那么,渲染选项卡部分,允许他们点击。
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?
有人有什么建议吗?
回答by Matthew Frederick
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:
在您登录视图控制器中,在对用户进行身份验证后,您可以像这样隐藏它:
[self.parentViewController.view setHidden:YES];
which allows you to show it again if you have a logout feature.
如果您有注销功能,它允许您再次显示它。
回答by Yuji
The standard way is the following:
标准方法如下:
- Package everything related to login screen into a view and a
UIViewController
subclass which manages that. Present that view modally in the app delegate in
application:didFinishLaunchingWithOptions:
by callingLoginController*loginController= ... ; // create the view controller for the login screen [self.tabController presentModalViewController:loginController animated:YES];
- 将与登录屏幕相关的所有内容打包成一个视图和一个
UIViewController
管理它的子类。 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
,但这对您来说可能有点困难。
One way which might be easier to understand (but uglier to my taste) is to define a method, say loginDone
in the app delegate.
Then, inside the LoginViewController
, you can do
一种可能更容易理解(但对我来说更难看)的方法是定义一个方法,比如loginDone
在应用程序委托中。然后,在里面LoginViewController
,你可以做
MyAppDelegate*appDelegate=[[UIApplication sharedApplication] delegate];
[appDelegate loginDone];
回答by Jumhyn
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
- Hook whatever you need up to IBOutlets in the AppDelegate, and write the method that checks if the password is valid.
- In the applicationDidFinishLaunching method, replace
[window addSubview:tabBarController.view];
with[window addSubview:/*whatever you called the view with the password stuff in it*/];
- If the user enters the correct password do this:
- 在 MainWindow.xib 中,创建一个 UIView,其中包含您想要在密码屏幕上显示的所有内容
- 将您需要的任何内容连接到 AppDelegate 中的 IBOutlets,并编写检查密码是否有效的方法。
- 在 applicationDidFinishLaunching 方法中,替换
[window addSubview:tabBarController.view];
为[window addSubview:/*whatever you called the view with the password stuff in it*/];
- 如果用户输入正确的密码,请执行以下操作:
[passView removeFromSuperview];
[window addSubview:tabBarController.view];
[passView removeFromSuperview];
[window addSubview:tabBarController.view];
And you should be in the regular tab bar application.
而且您应该在常规标签栏应用程序中。
回答by Tony
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
}
}