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

Posted

技术标签:

【中文标题】iphone - 将子视图添加到窗口后无法推送模态视图?【英文标题】:iphone - cant push modal views after adding subview to window? 【发布时间】:2011-05-12 17:32:00 【问题描述】:

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

模态视图将允许用户登录、查看条款和条件,然后如果他们接受,则调用 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 函数中,当我显示人员选择器时一切正常。

我在这里打破了一些基本的东西,或者任何人都可以发现我可能做错了什么?

【问题讨论】:

【参考方案1】:

您是否通过dismissModalViewControllerAnimated: 方法正确关闭了第一个模态视图控制器?如果您只是从其父视图中删除模态视图控制器的视图,那么 UIKit 可能会取消很多文​​书工作。

【讨论】:

【参考方案2】:

你真正应该做的是这样的:

    将 rootViewController 添加到 navigationController。然后将 navigationController 添加到窗口中。

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

    完成条款接受后,执行 [delegate loginViewControllerFinished];。

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


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


在登录视图控制器中:

// 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];


希望这会有所帮助。

【讨论】:

以上是关于iphone - 将子视图添加到窗口后无法推送模态视图?的主要内容,如果未能解决你的问题,请参考以下文章

在 iPhone 中使用模态视图将条形按钮添加到导航栏

在 ipad 中模态显示视图并在 iphone 中推送

iPad 模态表单视图不

Segue 不适用于动态添加的子视图

将子视图添加到屏幕问题

将子视图添加到 NSView 以获得类似国际象棋的网格