【问题标题】:How to create and get return Value from Cocoa Dialog?如何从 Cocoa Dialog 创建和获取返回值?
【发布时间】:2011-09-12 11:47:25
【问题描述】:

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

如何在 Cocoa、Objective-C 中做到这一点?

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

【问题讨论】:

  • 没有预定义的方法,因为那是糟糕的 UI。那应该只是一个字段。

标签: objective-c cocoa


【解决方案1】:

您可以调用一个 NSAlert 并将 NSTextField 作为它的附件视图,像这样"

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

【讨论】:

  • 如果你是通过 alloc-init 创建的,不要忘记释放 NSTextField。
  • @SebastianHojas 如果他们使用 ARC 进行内存管理,他们还需要这样做吗?
  • @rd108 使用 ARC 时不需要/不允许发布。
  • 是的,ARC 不需要任何版本。但是不要忘记在适当的时候使用@autoreleasepool。没有它,每个分配都由主池维护。因此,只有当主池耗尽时,所有实例才会从内存中释放。 Swift 或 Objective-C。
  • 此答案使用了已弃用的 API,通常显示其年龄。 (“[NSAlert alertWithMessageText ...] 等)。这些天只需使用 [[NSAlert alloc] init] 制作一个 NSAlert 并填写此处其他答案中描述的属性。
【解决方案2】:

在 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) {

    }

【讨论】:

    【解决方案3】:

    我相信您正在寻找的是一张纸。查看Sheet Programming Topics 文档

    我刚刚更新了一个Github Sample 项目。您可以在工作表上的字段中输入文本并将其传递回主窗口。

    这个例子展示了如何在一个 nib 中创建一个视图,并使用一个使用块作为回调的自定义工作表控制器类,而不必创建和传递一个选择器。

    【讨论】:

      【解决方案4】:

      从 Xcode 7.2.1 和 OS X 10.11 开始的 Swift 示例:

      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)\"")
          }
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-19
        • 2020-08-02
        • 1970-01-01
        相关资源
        最近更新 更多