在Cocoa Touch Tab Bar Application for IOS前添加login屏幕

仍然在这里我的头在附近的东西。 我甚至没有closures,但无论如何….我有一个从Xcode创build的TabBar应用程序。 它的作品,我有三个选项卡的意见,我知道如何操纵,等等。

我想在这个东西的前面放一个“login”nib文件,要求用户回答(现在的硬编码)用户名和密码。 如果你得到的是正确的,那么渲染标签部分,让他们点击左右。

我有另外一个应用程序,我写的是用户名和密码部分,我从那里拿出逻辑有困难,把它放在TabApplication的一块前面。

任何人有任何build议?

在你的AppDelegate中,在application didFinishLaunchingWithOptions方法的末尾,你会看到:

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

只需初始化您的login视图控制器,并将其添加到tabcontroller之后,如下所示:

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

在你loginviewcontroller,validation用户后,你可以像这样隐藏它:

 [self.parentViewController.view setHidden:YES]; 

如果您有注销function,您可以再次显示它。

标准的方法如下:

  • 将与login屏幕相关的所有内容打包到一个视图和一个pipe理它的UIViewController子类中。
  • application:didFinishLaunchingWithOptions:中的应用程序委托中以模态方式呈现该视图application:didFinishLaunchingWithOptions:通过调用

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

这样,过渡等之间的animation就会自动处理。

您可以稍后在成功login后将其解除。可以在LoginController通过

 [self.parentViewController dismissModalViewControllerAnimated:YES]; 

但是,一旦login完成,我经常需要做额外的设置。 所以,我会先告诉应用程序委托,login完成,然后执行

 [self.tabController dismissModalViewControllerAnimated:YES]; 

从应用程序代表。 然后我可以在那里执行额外的任务。

为了与应用程序委托进行通信,我会使用NSNotification ,但对您来说可能有点困难。

一种可能更容易理解的方式(但我的口味更加丑陋)是定义一个方法,例如在应用程序委托中的loginDone 。 然后,在LoginViewController里面,你可以做

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

如果你从默认的标签栏应用程序开始,你可以这样做:

  • 在MainWindow.xib中,创build一个UIView,其中包含您想要在密码屏幕上显示的所有内容
  • 把任何你需要的东西挂在AppDelegate的IBOutlets上,然后编写检查密码是否有效的方法。
  • 在applicationDidFinishLaunching方法中,replace[window addSubview:tabBarController.view];[window addSubview:/*whatever you called the view with the password stuff in it*/];
  • 如果用户input正确的密码,请执行以下操作:

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

你应该在常规的标签栏应用程序。

我更喜欢做以下事情:

在应用程序委托的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; 

然后获得authenticationcallback后,你可以在你的App Delegate里有这样的东西:

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