tags:

views:

86

answers:

1

Hi, Here's what I am trying to do. I need to prompt the user for a password prompt and until he enters the password and hits, say the Enter button on the sheet, I want to prevent the code being parsed in the background. Here's the code to run the sheet and when the user enters the password and hits Enter, endSpeedSheet is run. I am calling all of this from my Main() function.

What I am noticing is that the when the main function runs, the sheet shows up, the user is prompted for a password. But in the background, I already see " Code gets here" has been run. This means the code has already run in the background. What I need is the code to wait at the password prompt and then use this password after the Sheet has been dismissed. Any idea's on what I am missing here ? ( And thanks in advance :))

     - (IBAction) showSpeedSheet:(id)sender
    {

       [NSApp beginSheet:speedSheet
    modalForWindow:(NSWindow *)window
    modalDelegate:nil
    didEndSelector:nil
    contextInfo:nil];

    }

    -(IBAction)endSpeedSheet:(id)sender
    {
 joinPassword = [joinPasswordLabel stringValue];
 [NSApp endSheet:speedSheet];
 [speedSheet orderOut:sender];


    }


    -(IBAction)main:(id)sender
   { 
      [self showSpeedSheet:(id)sender];
      // More Code here
      NSLog(@" Code gets here");

   }
+1  A: 

The answer is simple: don't put the code that needs to run after the sheet completes directly after you call -showSpeedSheet:. Sheets are asynchronous, so you must factor your code so that it is only called once the sheet completes.

That's what the didEndSelector parameter in the ‑beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo: method is for. You pass in a selector to be called when the sheet is closed. In the modal delegate object, you implement this selector and do whatever processing needs to be done once the sheet completes.

- (IBAction) showSpeedSheet:(id)sender
{
    [NSApp beginSheet:speedSheet
       modalForWindow:self.window
        modalDelegate:self
       didEndSelector:@selector(speedSheetDidEnd:returnCode:contextInfo:)
          contextInfo:nil];
}


- (IBAction)endSpeedSheet:(id)sender
{
    self.joinPassword = [joinPasswordLabel stringValue];
    [NSApp endSheet:speedSheet];
    [speedSheet orderOut:sender];
}

- (void)speedSheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
    if(sheet == speedSheet)
    {
        //the sheet has ended, so do something
    }
}
Rob Keniger
Thanks Rob. That helped a lot. I got it working now. For anyone else who stumbles on this link, look this up too. I found it helpful:http://etutorials.org/Programming/Cocoa/Part+I+Introducing+Cocoa/Chapter+3.+The+Application+Kit/3.6+Sheets/
califguy