13

仍然让我的头脑围绕这里的事情。我什至没有接近,但无论如何......我有一个从 Xcode 创建的 TabBar 应用程序。它工作我有三个选项卡视图,我知道如何操作等。

我想在整个事情前面放一个“登录”nib 文件,要求用户回答(现在是硬编码的)用户名和密码。如果你做对了,那么,渲染选项卡部分,允许他们点击。

我有另一个应用程序,我已经编写了用户名和密码部分,我无法从那里获取逻辑,并将其放在 TabApplication 部分的前面。

有人有什么建议吗?

4

4 回答 4

17

在您的 AppDelegate 中,在application didFinishLaunchingWithOptions方法结束时您会看到:

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

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

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

在您登录视图控制器中,在验证用户身份后,您可以像这样隐藏它:

[self.parentViewController.view setHidden:YES];

如果您有注销功能,它允许您再次显示它。

于 2010-12-10T06:57:54.653 回答
16

标准方式如下:

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

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

这样,自动处理过渡等之间的动画。

您可以稍后在成功登录后将其关闭。可以从内部LoginController完成

[self.parentViewController dismissModalViewControllerAnimated:YES];

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

[self.tabController dismissModalViewControllerAnimated:YES];

来自应用委托。然后我可以在那里执行其他任务。

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

一种可能更容易理解(但对我来说更难看)的方法是定义一个方法,比如loginDone在应用程序委托中。然后,在里面LoginViewController,你可以做

MyAppDelegate*appDelegate=[[UIApplication sharedApplication] delegate];
[appDelegate loginDone];
于 2010-12-10T09:40:00.027 回答
2

如果您从默认选项卡栏应用程序开始,您可以这样做:

  • 在 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];

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

于 2010-12-10T07:42:04.370 回答
0

我更喜欢执行以下操作:

在 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;

然后在获得身份验证回调后,您可以在 App Delegate 中拥有类似的内容:

- (void)setAuthenticatedState:(BOOL)authenticated
{
    if (authenticated == YES) {
        dispatch_async(dispatch_get_main_queue(), ^(){
            self.window.rootViewController = self.tabBarController;
        });
    }else{
        // Stuff
    }
}
于 2013-04-06T02:20:18.617 回答