Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can presentModalViewController work at startup?

I'd like to use a modal UITableView at startup to ask users for password, etc. if they are not already configured. However, the command to call the uitableview doesn't seem to work inside viewDidLoad.

startup code:

- (void)viewDidLoad {
  rootViewController = [[SettingsController alloc] 
    initWithStyle:UITableViewStyleGrouped];
  navigationController = [[UINavigationController alloc]     
    initWithRootViewController:rootViewController];

  // place where code doesn't work
  //[self presentModalViewController:navigationController animated:YES];
}

However, the same code works fine when called later by a button:

- (IBAction)settingsPressed:(id)sender{
    [self presentModalViewController:navigationController animated:YES];
}

Related question: how do I sense (at the upper level) when the UITableView has used the command to quit?

[self.parentViewController dismissModalViewControllerAnimated:YES];
like image 859
BankStrong Avatar asked Jul 30 '09 18:07

BankStrong


1 Answers

You can place the presentModalViewController:animated: call elsewhere in code - it should work in the viewWillAppear method of the view controller, or in the applicationDidFinishLaunching method in the app delegate (this is where I place my on-launch modal controllers).

As for knowing when the view controller disappears, you can define a method on the parent view controller and override the implementation of dismissModalViewControllerAnimated on the child controller to call the method. Something like this:

// Parent view controller, of class ParentController
- (void)modalViewControllerWasDismissed {
    NSLog(@"dismissed!");
}

// Modal (child) view controller
- (void)dismissModalViewControllerAnimated:(BOOL)animated {
    ParentController *parent = (ParentController *)(self.parentViewController);
    [parent modalViewControllerWasDismissed];

    [super dismissModalViewControllerAnimated:animated];
}
like image 197
Tim Avatar answered Oct 13 '22 07:10

Tim