iphone - 在向窗口添加子视图后无法推送模态视图?

时间:2011-05-12 17:32:00

标签: iphone ios uinavigationcontroller presentmodalviewcontroller abpeoplepickerview

我的应用程序我有以下代码,第一个位在我的应用程序启动时运行,它在模态视图中显示登录屏幕。在我之前将rootController添加到窗口中。

Modal视图将允许用户登录,查看条款和条件,然后如果他们接受,则调用loadMainApp函数。

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

    LoginViewController *_loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginView" bundle:[NSBundle mainBundle]];
    self.loginViewController = _loginViewController;
    [_loginViewController release];


    UINavigationController *navi = [[[UINavigationController alloc]initWithRootViewController:loginViewController]autorelease];

    navi.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [loginViewController release];
    [rootController presentModalViewController:navi animated:YES];

    [self.window makeKeyAndVisible];
    return YES;
}

当用户接受条款和条件时,将调用下一位:

-(void)loadMainApp:(UIView *)fromView{

    [fromView removeFromSuperview];
    [window addSubview:rootController.view];
    rootController.selectedIndex = 2;
    rootController.delegate = self;

}

从这里开始,我希望能够使用人物选择器,这是一个模态视图,我尝试使用此代码:

 ABPeoplePickerNavigationController *picker =
        [[ABPeoplePickerNavigationController alloc]init];
        picker.peoplePickerDelegate = self;

        [rootController presentModalViewController:picker animated:YES];
        [picker release];

但是没有任何反应,当我尝试从rootController呈现模态视图时,没有任何显示。

这似乎是在运行applicationDidFinishLaunching中的此代码之后发生的:

LoginViewController *_loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginView" bundle:[NSBundle mainBundle]];
        self.loginViewController = _loginViewController;
        [_loginViewController release];


        UINavigationController *navi = [[[UINavigationController alloc]initWithRootViewController:loginViewController]autorelease];

        navi.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [loginViewController release];
        [rootController presentModalViewController:navi animated:YES];

        [self.window makeKeyAndVisible];
        return YES;

如果我根本不显示登录屏幕,而只是添加我的rootController

rootController.delegate = self;
        [window addSubview:rootController.view];
在applicationDidFinishLaunching函数中

,当我去显示人物选择器时,一切正常。

我有什么基本的东西在这里打破,或者有人能发现我可能做错了吗?

2 个答案:

答案 0 :(得分:2)

您是否通过dismissModalViewControllerAnimated:方法正确解雇了第一个模态视图控制器?如果您只是从超级视图中删除模态视图控制器的视图,那么UIKit可能会撤消大量文书工作。

答案 1 :(得分:1)

你应该做的是这样的事情:

  1. 将rootViewController添加到navigationController。然后将navigationController添加到窗口。

  2. 在rootViewController的viewDidLoad方法中,将loginViewController的委托设置为rootViewController,并将LoginViewController的实例显示为模态。

  3. 完成条款验收后,请[委托loginViewControllerFinished];。

  4. 现在在rootViewController中,处理loginViewControllerFinished方法。这应该包含对[self dismissModalViewControllerAnimated:YES]的调用;关闭登录模式视图。然后你应该启动ABPeoplePickerNavigationController 作为模态视图


  5. 在AppDelegate中:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // Make sure rootViewController is initiated by this point.
    
        UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease];
    
        [window addSubview:rootController.view];
        [self.window makeKeyAndVisible];
        return YES;
    }
    

    在RootViewController中:

    - (void)viewDidLoad {
      [super viewDidLoad];
    
      LoginViewController *loginViewController = [[[LoginViewController alloc] initWithNibName:@"LoginView" bundle:nil] autorelease];
      loginViewController.delegate = self;
    
      loginViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
      [self presentModalViewController:loginViewController animated:YES];
    }
    
    - (void)loginViewControllerFinished {
      [self dismissModalViewControllerAnimated:YES];
    
      // Here we are closing one modal. And showing another after that.
      ABPeoplePickerNavigationController *peoplePicker = [[[ABPeoplePickerNavigationController alloc] init] autorelease];
      peoplePicker.peoplePickerDelegate = self;
      [self presentModalViewController:peoplePicker animated:YES];
    }
    

    在LoginViewController中:

    // Make sure LoginViewController has delegate property in header.
    // @property (nonatomic, assign) id delegate;
    // And put this in implementation (.m) file. @synthesize delegate.
    // Don't put release for delegate, since it's not retained. It's only 'assigned'.
    
    - (void)done {
      // Call this when you want to close loginViewController.
      [delegate loginViewControllerFinished];
    }
    

    希望这有帮助。