objective-c - 如何从 Cocoa Dialog 中创建和获取返回值?

标签 objective-c cocoa

在我的应用程序中,我想创建一个带有一个文本字段和一个按钮的对话框, 通过它我可以提示用户并取回用户输入的值。

我如何在 Cocoa、Objective-C 中执行此操作?

我没有找到任何预定义的方法。

最佳答案

您可以调用 NSAlert 并将 NSTextField 作为 accessoryView 像这样放置"

- (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;
    }
}

关于objective-c - 如何从 Cocoa Dialog 中创建和获取返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7387341/

相关文章:

objective-c - 连接到远程数据库

cocoa - NSTextField 带阴影?

Objective-C 类别不被识别?

iphone - <AppName>.pch 文件使用

iphone - 如何将本地视频文件嵌入到 Html 中以在 UIWebView 中播放?

ios - 从主视图 Controller 访问模态视图 Controller 中 UITextField 的文本

objective-c - 如何计算 NSMutableArray 的每个内容的哈希 MD5?

ios - UIViewController 初始加载时高度错误

iphone - 要求用户先通过登录屏幕才能访问 iPhone 应用程序?

cocoa - 通过绑定(bind)和值转换器在 View (NSTableColumn)中显示一对多关系