How to create and get return Value from Cocoa Dialog?

16,848

Solution 1

You can call an NSAlert and put the NSTextField as it's accessoryView like this"

- (NSString *)input: (NSString *)prompt defaultValue: (NSString *)defaultValue {
    NSAlert *alert = [NSAlert alertWithMessageText: prompt
                                     defaultButton:@"OK"
                                   alternateButton:@"Cancel"
                                       otherButton:nil
                         informativeTextWithFormat:@""];

    NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
    [input setStringValue:defaultValue];
    [input autorelease];
    [alert setAccessoryView:input];
    NSInteger button = [alert runModal];
    if (button == NSAlertDefaultReturn) {
        [input validateEditing];
        return [input stringValue];
    } else if (button == NSAlertAlternateReturn) {
        return nil;
    } else {
        NSAssert1(NO, @"Invalid input dialog button %d", button);
        return nil;
    }
}

Solution 2

IN OS X 10.10:

    NSAlert *alert = [[NSAlert alloc] init];
    [alert setMessageText:@"Permission denied, sudo password?"];
    [alert addButtonWithTitle:@"Ok"];
    [alert addButtonWithTitle:@"Cancel"];

    NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
    [input setStringValue:@""];

    [alert setAccessoryView:input];
    NSInteger button = [alert runModal];
    if (button == NSAlertFirstButtonReturn) {
        password = [input stringValue];
    } else if (button == NSAlertSecondButtonReturn) {

    }

Solution 3

An example in Swift as of Xcode 7.2.1 and OS X 10.11:

let a = NSAlert()
a.messageText = "Please enter a value"
a.addButtonWithTitle("Save")
a.addButtonWithTitle("Cancel")

let inputTextField = NSTextField(frame: NSRect(x: 0, y: 0, width: 300, height: 24))
inputTextField.placeholderString = "Enter string"
a.accessoryView = inputTextField

a.beginSheetModalForWindow(self.window!, completionHandler: { (modalResponse) -> Void in
    if modalResponse == NSAlertFirstButtonReturn {
        let enteredString = inputTextField.stringValue
        print("Entered string = \"\(enteredString)\"")
    }
})

Solution 4

I believe what you are looking for is a sheet. Have a look at the Sheet Programming Topics documentation

I've just updated a Github Sample project on this. You can enter text in a field on the sheet and pass that back to the main window.

This example shows how to create a view in a nib and use a custom sheet controller class which uses a block as the callback, rather than having to create and pass in a selector.

Share:
16,848

Related videos on Youtube

udr1990
Author by

udr1990

@indravardhanslinkedin SOreadytohelp

Updated on June 04, 2022

Comments

  • udr1990
    udr1990 almost 2 years

    In my application, I want to create a dialog box with one text field, and a button, through which I can prompt user and get back user entered value.

    How do I do this in Cocoa, Objective-C ?

    I didn't find any predefined method for that.

    • Nico
      Nico over 12 years
      There isn't a predefined method for that because that's bad UI. That should just be a field.
  • Sebastian Hojas
    Sebastian Hojas almost 12 years
    Don't forget to release the NSTextField if you are creating it by alloc-init.
  • rd108
    rd108 almost 11 years
    @SebastianHojas do they still need to do that if their using ARC for memory management?
  • Sebastian Hojas
    Sebastian Hojas almost 11 years
    @rd108 No release is needed/allowed when you are using ARC.