在主标签栏控制器之前显示登录视图控制器 [英] Showing login view controller before main tab bar controller

查看:95
本文介绍了在主标签栏控制器之前显示登录视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个带有标签栏控制器的iPad应用程序,需要登录。所以在启动时,我想显示一个LoginViewController,如果登录成功,则显示标签栏控制器。这就是我实现初始测试版本的方法(省略了一些典型的标题内容等)......

I'm creating an iPad app with a tab bar controller that requires login. So on launch, I want to show a LoginViewController and if login is successful, then show the tab bar controller. This is how I implemented an initial test version (left out some typical header stuff, etc)...

AppDelegate.h:

AppDelegate.h:

@interface AppDelegate_Pad : NSObject 
        <UIApplicationDelegate, LoginViewControllerDelegate> {
    UIWindow *window;
    UITabBarController *tabBarController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end

AppDelegate.m:

AppDelegate.m:

@implementation AppDelegate_Pad
@synthesize window;
@synthesize tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    LoginViewController_Pad *lvc = [[LoginViewController_Pad alloc] initWithNibName:@"LoginViewController_Pad" bundle:nil];
    lvc.delegate = self;
    [window addSubview:lvc.view];
    //[lvc release];
    [window makeKeyAndVisible];
    return YES;
}
- (void)loginViewControllerDidFinish:(LoginViewController_Pad *)loginViewController {
    [window addSubview:tabBarController.view];
}
- (void)dealloc {...}
@end

LoginViewController_Pad.h:

LoginViewController_Pad.h:

@protocol LoginViewControllerDelegate;
@interface LoginViewController_Pad : UIViewController {
    id<LoginViewControllerDelegate> delegate;
}
@property (nonatomic, assign) id <LoginViewControllerDelegate> delegate;
- (IBAction)buttonPressed;
@end
@protocol LoginViewControllerDelegate
-(void)loginViewControllerDidFinish:(LoginViewController_Pad *)loginViewController;
@end

LoginViewController_Pad.m:

LoginViewController_Pad.m:

@implementation LoginViewController_Pad
@synthesize delegate;
    ...
- (IBAction)buttonPressed
{
    [self.view removeFromSuperview];
    [self.delegate loginViewControllerDidFinish:self];
}
    ...
@end

所以应用程序delegate在启动时添加登录视图控制器的视图,并等待登录使用委托调用done finish。登录视图控制器在调用didFinish之前调用removeFromSuperView。然后app appate在标签栏控制器的视图上调用addSubView。

So the app delegate adds the login view controller's view on launch and waits for login to call "did finish" using a delegate. The login view controller calls removeFromSuperView before it calls didFinish. The app delegate then calls addSubView on the tab bar controller's view.

如果你做到了这一点,谢谢,我有三个问题:

If you made it up to this point, thanks, and I have three questions:


  1. 主要问题:这是在显示应用主标签栏控制器之前显示视图控制器的正确方法吗?即使它似乎有效,它是一种正确的方法吗?

  1. MAIN QUESTION: Is this the right way to show a view controller before the app's main tab bar controller is displayed? Even though it seems to work, is it a proper way to do it?

如果我在app delegate中注释掉lvc release,那么当按下登录视图控制器上的按钮时,应用程序会与EXC_BAD_ACCESS崩溃。为什么?

If I comment out the "lvc release" in the app delegate then the app crashes with EXC_BAD_ACCESS when the button on the login view controller is pressed. Why?

随着lvc release注释掉一切似乎都有效但是在调试器控制台上,当app委托为标签栏控制器调用addSubView时它会写入此消息:使用两阶段旋转动画。要使用更流畅的单阶段动画,此应用程序必须删除两阶段方法实现。这意味着什么,我需要担心它吗?

With the "lvc release" commented out everything seems to work but on the debugger console it writes this message when the app delegate calls addSubView for the tab bar controller: Using two-stage rotation animation. To use the smoother single-stage animation, this application must remove two-stage method implementations. What does that mean and do I need to worry about it?

更新:
正如lucius所建议的那样,将其更改为从app delegate模态显示登录视图控制器。这似乎是一个更清洁的解决方案。代码更改如下...

UPDATE: As suggested by lucius, changed it to modally show the login view controller from the app delegate. This appears to be a cleaner solution. Code changed as follows...

AppDelegate.m:

AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [window addSubview:tabBarController.view];
    [window makeKeyAndVisible];

    LoginViewController_Pad *lvc = [[LoginViewController_Pad alloc] initWithNibName:@"LoginViewController_Pad" bundle:nil];
    lvc.delegate = self;
    [self.tabBarController presentModalViewController:lvc animated:NO];
    [lvc release];

    return YES;
}
-(void)loginViewControllerDidFinish:(LoginViewController_Pad *)loginViewController {
    [self.mainTabBarController dismissModalViewControllerAnimated:NO];
}

LoginViewController_Pad.m:

LoginViewController_Pad.m:

- (IBAction)buttonPressed
{
    //do NOT removeFromSuperview, delegate will dismiss
    //[self.view removeFromSuperview];
    [self.delegate loginViewControllerDidFinish:self];
}


推荐答案

我会用这个方法以模态方式呈现视图控制器而不是将其添加到窗口。这将使其正确保留控制器。调试器消息与您的类中实现的某些自动旋转方法有关。你现在可以忽略它。

I'd use the method to present the view controller modally instead of adding it to the window. That will make it properly retain the controller. The debugger message has to do with certain autorotation methods being implemented in your class. You can ignore it for now.

这篇关于在主标签栏控制器之前显示登录视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆