1

I have a pop up button that is loaded with 5 options programicically, if a particular option is selected, lets say, "append the end of file name",then my update function needs to cause a small pop up window with a textField and a save and cancel button to appear. I don't know how to do this. I was able to get a nice NSAlert example to work but it does not take a textField that I know of. Is there a dialog class or some other modal I should be using or should I be trying to create a second nib? in either case I don't really know how to do it so a good example or tutorial would be great.

Thanks

Miek
  • 1,127
  • 4
  • 20
  • 35

1 Answers1

5

I just searched around, and there is a method someone found to display an NSAlert with an NSTextField, buttons and get the text the user has just typed. It's here, on the Macrumors forums, slightly old..

Essentially you could just go with:

NSString *prompt = @"Please enter text to append to file name:";
NSString *infoText = @"What happens here is...";
NSString *defaultValue = @"Default Value";

NSAlert *alert = [NSAlert alertWithMessageText: prompt
                                 defaultButton:@"Save"
                               alternateButton:@"Cancel"
                                   otherButton:nil
                     informativeTextWithFormat:infoText];

NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
[input setStringValue:defaultValue];
[alert setAccessoryView:input];
NSInteger button = [alert runModal];
if (button == NSAlertDefaultReturn) {
    [input validateEditing];
    NSLog(@"User entered: %@", [input stringValue]);
} else if (button == NSAlertAlternateReturn) {
    NSLog(@"User cancelled");
} else {
    NSLog(@"bla");
}

That code would display the NSAlert, with customisable prompt, informative text and default value for the NSTextField, plus log what the user entered, whether they cancelled, etc.

Hope that works! :)

Seb Jachec
  • 3,003
  • 2
  • 30
  • 56
  • I tried this code and it looks pretty professional even if it is old. Two things I'm not getting though is a setting for location relative to the window that opened it(I read a post that said that requires to move to a "sheet"?) and a size setting for the box(much less important) – Miek Oct 26 '11 at 16:50