代码之家  ›  专栏  ›  技术社区  ›  Kristopher Johnson

在Cocoa中取消模式对话框之前强制读取文本字段

  •  2
  • Kristopher Johnson  · 技术社区  · 17 年前

    那么,当编辑“不完整”时,如何强制更改的字段影响绑定值?

    通过阅读文档,我认为我可能能够在关闭对话框之前为每个文本字段调用NSControl的validatediting方法,但似乎应该有一种更简单的方法。

    - (void)showDialog {
        [NSApp activateIgnoringOtherApps:YES];
        [NSApp beginSheet:startTimerDialog
           modalForWindow:nil
            modalDelegate:nil
           didEndSelector:nil
              contextInfo:nil];
        [NSApp runModalForWindow:startTimerDialog];
        [NSApp endSheet:startTimerDialog];
        [startTimerDialog orderOut:self];
    }
    

    确定按钮(实际标题为“开始”)针对此方法:

    - (IBAction)startTimerDialogStartButtonWasClicked:(id)sender {
        [self closeModalDialog:sender];
    
        // Then, call methods that read values from NSUserDefaults
        // ...    
    }
    
    4 回复  |  直到 17 年前
        1
  •  4
  •   Peter Hosey    17 年前

    将用户默认值发送到控制器 commitEditing 消息

        2
  •  2
  •   Kristopher Johnson    17 年前

    [startTimerDialog makeFirstResponder:nil];
    

    根据文档,这会将resignFirstResponder发送给当前的第一响应者,这将导致编辑结束。

        3
  •  1
  •   Dirk Stoop    17 年前

    通过以下调用可以实现稍微突然结束编辑的方式:

    [startTimerDialog endEditingFor:nil];
    

    (有关“密集型”的更正式描述,请参见下面的文档链接) .

    简言之: -招标人:

    更微妙的是 NSWindow class documentation -招标人: 苹果提出了类似的建议:

    if ( ![startTimerDialog makeFirstResponder:startTimerDialog] )
        /* Making the window firstResponder failed, proceed to
           force the first responder to resign */
    
        [startTimerDialog endEditingFor:nil];
    

    按照上面的链接查看他们的确切示例代码,我在这里将其缩短,因为您显然对在所描述的用例中使用fieldEditor不感兴趣。

        4
  •  0
  •   Abizern    17 年前

    delegate 用于所有文本字段,并使其直接处理更改。