2011-09-07 59 views
0

基本上我想在显示我的TabBarController之前在我的iPhone应用程序中创建一个登录屏幕。我尝试了以下方法,首先添加到窗口子视图我的TabBarController和顶部我的LoginViewController。我做错了什么,或者我应该完全不同?将一个ViewController放在TabBarController的顶部

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{  
    // Override point for customization after application launch. 
    [self.window makeKeyAndVisible]; 

    NSMutableArray *tabItems = [[NSMutableArray alloc] initWithCapacity:2]; 

    DefaultViewController *dvc = [[DefaultViewController alloc] init]; 
    UINavigationController *dvc_nc = [[UINavigationController alloc] initWithRootViewController:dvc]; 
    dvc_nc.tabBarItem.title = @"Home"; 
    //dvc_nc.tabBarItem.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Default" ofType:@"png"]]; 
    [tabItems addObject:dvc_nc]; 
    [dvc release]; 
    [dvc_nc release]; 

    OptionsViewController *ovc = [[OptionsViewController alloc] initWithStyle:UITableViewStyleGrouped]; 
    UINavigationController *ovc_nc = [[UINavigationController alloc] initWithRootViewController:ovc]; 
    ovc_nc.tabBarItem.title = @"Option"; 
    //ovc_nc.tabBarItem.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Option" ofType:@"png"]]; 

    [tabItems addObject:ovc_nc]; 
    [ovc release]; 
    [ovc_nc release]; 

    UITabBarController *tbc = [[UITabBarController alloc] init]; 
    tbc.viewControllers = tabItems; 
    self.tabController = tbc; 
    [tabItems release]; 
    [tbc release]; 

    [self.window addSubview:self.tabController.view]; 

    LoginViewController *lvc = [[OptionsViewController alloc] init]; 
    UINavigationController *lvc_nc = [[UINavigationController alloc] initWithRootViewController:lvc]; 
    [self.window addSubview:lvc_nc.view]; 
    [lvc release]; 
    [lvc_nc release]; 

    return YES; 
} 

回答

3
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{  
    // Override point for customization after application launch. 
    [self.window makeKeyAndVisible]; 

    NSMutableArray *tabItems = [[NSMutableArray alloc] initWithCapacity:2]; 

    DefaultViewController *dvc = [[DefaultViewController alloc] init]; 
    UINavigationController *dvc_nc = [[UINavigationController alloc] initWithRootViewController:dvc]; 
    dvc_nc.tabBarItem.title = @"Home"; 
    //dvc_nc.tabBarItem.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Default" ofType:@"png"]]; 
    [tabItems addObject:dvc_nc]; 
    [dvc release]; 
    [dvc_nc release]; 

    OptionsViewController *ovc = [[OptionsViewController alloc] initWithStyle:UITableViewStyleGrouped]; 
    UINavigationController *ovc_nc = [[UINavigationController alloc] initWithRootViewController:ovc]; 
    ovc_nc.tabBarItem.title = @"Option"; 
    //ovc_nc.tabBarItem.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Option" ofType:@"png"]]; 

    [tabItems addObject:ovc_nc]; 
    [ovc release]; 
    [ovc_nc release]; 

    UITabBarController *tbc = [[UITabBarController alloc] init]; 
    tbc.viewControllers = tabItems; 
    self.tabController = tbc; 
    [tabItems release]; 
    [tbc release]; 

    [self.window addSubview:self.tabController.view]; 

    LoginViewController *lvc = [[OptionsViewController alloc] init]; 
    UINavigationController *lvc_nc = [[UINavigationController alloc] initWithRootViewController:lvc]; 
[self.tabController presentModalViewController:lvc_nc animated:no]; 
    [lvc release]; 
    [lvc_nc release]; 

    return YES; 
} 
+0

我昨天有同样的问题,并且@rckoenes写的'presentModalViewController'是正确的并且工作的解决方案 – beny

+0

啊哈,看起来太工作了!这是在完成它之后删除LoginView的正确方法吗? [self.parentViewController dismissModalViewControllerAnimated:YES]; – Wesley

+0

'[self dismissModalViewControllerAnimated:YES];'会做得很好。 – rckoenes

0

我会使用两种不同的视图。一个会处理登录过程,一个是你的“登录视图”,它提供了你的应用程序的功能。当您的应用程序启动时,您添加登录视图,检查用户名/密码,以及登录顺利时切换到第二个视图。

+0

你有一个与我的tabbarcontroller一起工作的代码示例吗? – Wesley

+0

没有抱歉,我在工作,无法提供代码示例:/从逻辑:只需创建2个不同的UIViewControllers,其中有你的登录表单,并获得的呈现/添加到你的applicationDidFinishLaunching方法。在那里放置一个方法,验证输入的用户凭证,并在用户名/密码正确的情况下显示第二个视图(其中包含UITabBar)。 –

相关问题