如何使用登录功能开发基于TabBar的应用程序?

时间:2012-05-26 10:13:35

标签: iphone ios login tabbarcontroller

我正在开发一个应用程序,我需要向用户显示一个列表作为菜单(课程,课程,成绩,注销)。所以即使在此之前我需要显示登录屏幕。只有成功有效登录后,我才需要将用户重定向到菜单。所以我计划开发一个带有4个选项卡的基于tabBar的应用程序。在加载TabBar控制器之前,我对如何添加登录视图控制器感到困惑。我想每次都选择第一个标签。截至目前,我将 TabBar控制器作为rootviewcontroller添加到我的 AppDelegate窗口,然后将登录视图控制器呈现为模态视图控制器。但是这里的问题甚至在加载登录视图控制器之前,我的课程视图控制器已加载,因为首先加载了tabbarcontroller。我的实际要求是我需要根据Login View控制器中给出的输入加载课程视图控制器和课程列表。但是在登录视图控制器的加载视图之前加载了课程视图控制器的加载视图。所以我的课程列表总是一样的,不管是谁登录。我在这里很困惑如何向前推进......这里的任何建议都会有很大的帮助...

3 个答案:

答案 0 :(得分:2)

所以,一个非常简单的例子,可能是;在你的loginViewController中你应该有一些像这样的方法:

//Call this after the user has done with the login
-(IBAction)remove:(id)sender{
    AppDelegate *del=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    //Set some data based on the user's input (eg some property shared in the AppDelegate)
    //del.dataEnterByTheUser=someData;
    [del removeLoginView];
} 

然后在AppDelegate中(假设现在 rootViewControllerloginViewController)你可以这样做(你可以优化转换):< / p>

-(void)removeLoginView{

    UITabBarController *tabVC=[[UITabBarController alloc] init];
    ViewController *v1=[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    //v1.data=self.dataEnterByTheUser;
    ViewController *v2=[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    NSArray *arrayVC=[NSArray arrayWithObjects:v1,v2, nil];
    [tabVC setViewControllers:arrayVC];
    [tabVC setSelectedViewController:0];
    CGRect rectVC=self.loginViewController.view.frame;
    rectVC.origin.y=self.view.frame.size.height;
    [UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.loginViewController.view.frame=rectVC;
    } completion:^(BOOL finished){
        [self.loginViewController.view removeFromSuperview];
        self.loginViewController=nil;
        self.window.rootViewController=tabVC;
    }];    
}

还记得在每个viewControllers的initWithNibName:中设置self.title以在tabItem上设置标题。

答案 1 :(得分:2)

无需摆弄rootViewController ......

只需在视图控制器的viewWillAppear:方法的开头添加以下代码,该方法通常首先出现(很可能是您在第一个标签中显示的VC):

[self.tabBarController presentModalViewController:loginController animated:NO];

其中loginController显然是管理登录屏幕的视图控制器。如果您在没有动画的情况下显示它,它将是您的应用程序启动时可见的第一件事(默认图像消失后)。我使用了相同的方法来显示用户在使用应用程序之前必须阅读的免责声明页面。它工作正常,没有问题就可以到商店。

编辑:在此解决方案中,一旦用户成功登录,loginController必须自行解除:

[self dismissModalViewControllerAnimated:NO]; //Although you might do this animated, this time

答案 2 :(得分:1)

您可以在运行时更改选项卡栏控制器中的视图控制器数组。这应该足以满足您的目的。

我写了一个小例子。尝试使用以下凭据登录:

  • 用户名:john,密码:doe
  • 用户名:pete,密码:poe

根据使用的登录信息,您会看到不同的标签组合。

该示例可以从我的Dropbox下载:http://dl.dropbox.com/u/6487838/LoginTabExample.zip

相关问题