views:

1110

answers:

4

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];
A: 

If you are going to do it like that then you are going to have to declare your own protocol to be able to tell when the UITableView dismissed the parentViewController, so you declare a protocol that has a method like

-(void)MyTableViewDidDismiss

then in your parent class you can implement this protocol and after you dismissModalView in tableView you can call MyTableViewDidDismiss on the delegate (whihc is the parent view controller).

Daniel
+2  A: 

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];
}
Tim
tried viewWillAppear and got the same bad effect as viewdidload. When I use [self presentModalViewController:navigationController animated:YES]; in applicationDidFinishLaunching, I get "SIGABRT". I'm probably calling it wrong
BankStrong
Can't call it from `applicationDidFinishLaunching` - that's a method belonging to the delegate, which is not a view controller. You need the root view controller object. Oftentimes it's automatically created for you by Xcode; look for a synthesized property called `viewController`, `rootController`, or `tabBarController`, depending on your app type.
Tim
That may have been unclear (sorry!); what I meant was in `applicationDidFinishLaunching` you can't run `[self present...]` - instead you have to run `[self.viewController present...]` or similar, depending on the name of your view controller object.
Tim
+1  A: 

I had quite the same problem. I know the topic is old but maybe my solution could help someone else... You just have to move your modal definition in a method:

// ModalViewController initialization
- (void) presentStartUpModal 
{
    ModalStartupViewController *startUpModal = [[ModalStartupViewController alloc] initWithNibName:@"StartUpModalView" bundle:nil];
    startUpModal.delegate = self;

    [self presentModalViewController:startUpModal animated:YES];
    [startUpModal release]; 
}

Next, in viewDidLoad, call your modal definition method in a performSelector:withObject:afterDelay: with 0 as delay value. Like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    //[self presentStartUpModal];  // <== This line don't seems to work but the next one is fine.
    [self performSelector:@selector(presentStartUpModal) withObject:nil afterDelay:0.0];
}

I still don't understand why the 'standard' way doesn't work.

dhar
Thank you for posting this. I've been trying to get this to work for days and this has solve a similar problem of mine. Like you, I've no idea why the first one doesn't work but the second does - but still problem solved.
gnuchu
A: 

optionally it should work in viewDidAppear without the workaround.

Hardway