Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

youngplayer

macrumors member
Original poster
May 16, 2008
36
0
Shanghai,China
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.
 
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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.