1

I have an IBAction and it looks like this.

- (IBAction) onPressed: (id) sender {

   [self openMyDelegateToSeeIfIAmReady];

   if (AmIReady == YES)
   {
      [self doMyWork];
   }
}

Right now, this doesn't work. AmIReady is a boolean and it changes to YES in openMyDelegateToSeeIfIAmReady. The thing is, before AmIReady becomes YES, this chunk of code

if (AmIReady == YES)
{
   [self doMyWork];
}

gets called and doMyWork never gets invoked. How can I make this method wait until it finishes [self openMyDelegateToSeeIfIAmReady]?

EDIT:

Here is what I have in openMyDelegateToSeeIfIAmReady

- (void) openMyDelegateToSeeIfIAmReady
{
    MyViewController *mvc = [[MyViewController alloc]initWithNibName:@"MyViewController" bundle:nil];
    mvc.delegate  = self;

    [self presentModalViewController:mvc animated:YES];
    [mvc release];
    amIReady = YES;
}

Also in this delegate (MyViewCrontroller), it requires user's input. If the user input has been entered, I need to run doMyWork.

Rocky
  • 289
  • 2
  • 3
  • 11
  • 1
    It already does wait. – trojanfoe Feb 26 '13 at 08:36
  • 1
    if you are not running that method on another thread then that function will finish before it gets to the if statement – Fonix Feb 26 '13 at 08:43
  • 3
    it should wait; anyways, can you post code for `openMyDelegateToSeeIfIAmReady`? – viral Feb 26 '13 at 08:43
  • @rocky:+1 You are correct, i faced many a times similar kind of problem while developing for osx, as you haven't mentioned your target(osx or ios). – Anoop Vaidya Feb 26 '13 at 09:05
  • We would need to see the source of `openMyDelegateToSeeIfIAmReady` in order to answer this question. – ipmcc Feb 26 '13 at 12:25
  • please use ARC . – nielsbot Feb 26 '13 at 18:52
  • as the others say, it already waits... although `presentModalViewController:animated:` will kick off an animation which will not be completed when that method returns. Do you want to wait for the modal view controller to be completely presented? This is the wrong approach... – nielsbot Feb 26 '13 at 18:54
  • @nielshot This project needs to be done without ARC unfortunately. I am not used to it either. – Rocky Feb 26 '13 at 18:56
  • not used to it: no time like the present! ARC will (mostly) save you lots of work and memory leaks. – nielsbot Feb 26 '13 at 18:59

1 Answers1

0

If you want to have some code run after your modal view controller has been presented, use -[ UIViewController presentViewController:animated:completion:] and pass a block to the 'completion' argument.

(This method is the replacement for presentModalViewController:animated:)

Change your code to:

-(IBAction)onPressed:(id)sender 
{
    MyViewController * controller = [ [ MyViewController alloc ] initWithNibName:@"MyViewController" bundle:nil ];
    controller.delegate = self;
    [ self presentViewController:controller animated:YES completion:^{
        [ self doMyWork ] ;
    }]
}
nielsbot
  • 15,922
  • 4
  • 48
  • 73
  • Very close. I am actually trying to run `doMyWork` after my modal view controller has been `viewWillDisappear:`ed as opposed to `viewWillAppear:`ed – Rocky Feb 26 '13 at 19:13
  • then you need to have your model view controller send a message to your delegate after it is dismissed... – nielsbot Feb 26 '13 at 19:40