How to implement an alert sheet with an input text field in Cocoa?

Discussion in 'Mac Programming' started by youngplayer, Jul 6, 2008.

  1. youngplayer macrumors member

    Joined:
    May 16, 2008
    Location:
    Shanghai,China
    #1
    How to implement an alert sheet with an input text field?
    For example, show an alert sheet to user to input password.

    I just know the simple usage of NSBeginAlertSheet, and I haven't read any article about my need. Please give me some advice or article reference. Thanks in advance.
     
  2. youngplayer thread starter macrumors member

    Joined:
    May 16, 2008
    Location:
    Shanghai,China
    #2
    I've found a method:
    - (NSString *)input: (NSString *)prompt defaultValue: (NSString *)defaultValue {
    NSAlert *alert = [NSAlert alertWithMessageText: prompt
    defaultButton:mad:"OK"
    alternateButton:mad:"Cancel"
    otherButton:nil
    informativeTextWithFormat:mad:""];

    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];
    return [input stringValue];
    } else if (button == NSAlertAlternateReturn) {
    return nil;
    } else {
    NSAssert1(NO, @"Invalid input dialog button %d", button);
    return nil;
    }
    }

    But this method's performance is not as good as I want. It's alert window is seperated from the main window. How can I make it shown like a sheet from the main window?
     

Share This Page