View Full Version : How to implement an alert sheet with an input text field in Cocoa?
youngplayer
Jul 6, 2008, 09:39 PM
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.
youngplayer
Jul 6, 2008, 11:39 PM
I've found a method:
- (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];
[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?
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.