开发者

iphone - cant push modal views after adding subview to window?

Im my application I have the following code, the first bit runs when my application launches, and it presents a login screen in a modal view. With my rootController added to the window before that.

The Modal view will allow a user to log on, view terms and conditions and then if they accept the loadMainApp function is called.

- (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;
}
开发者_如何学Go

This next bit is called when the user accepts terms and conditions:

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

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

}

From here I want to be able to use the people picker which is a modal view and I attempt it with this code:

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

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

But nothing happens, when I try to present a modal view from my rootController nothing displays.

This appears to occur after this code in applicationDidFinishLaunching is run:

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;

If I dont display the login screen at all and instead just add my rootController

rootController.delegate = self;
        [window addSubview:rootController.view];

in the applicationDidFinishLaunching function then everything works fine when I go to display the people picker.

Is there something fundamental that I am breaking here or can anyone spot what I may be doing wrong?


Are you dismissing the first modal view controller correctly, via thedismissModalViewControllerAnimated: method? If you're just removing the modal view controller's view from its superview, then probably a lot of paperwork is being left undone by UIKit.


What you should really do is something like this:

  1. Add rootViewController to navigationController. Then add navigationController to the window.

  2. In rootViewController's viewDidLoad method, set loginViewController's delegate to rootViewController and display the LoginViewController's instance as a modal.

  3. When you are done with terms acceptance, do [delegate loginViewControllerFinished];.

  4. Now in rootViewController, handle the loginViewControllerFinished method. This should contain a call to [self dismissModalViewControllerAnimated:YES]; to close the login modal view. Then you should launch ABPeoplePickerNavigationController as a modal view.


In 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;
}

In 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];
}

In 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];
}

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜