【问题标题】:Add "informative text" to Alert if TextFields are empty如果 TextFields 为空,则将“信息性文本”添加到警报
【发布时间】:2015-09-07 15:42:46
【问题描述】:

我正在尝试编写一个 NSAlert,当某些 NSTextFields 为空时出现。 我有 3 个 NSTextField,我想要一个 NSAlert 来显示列表中哪个 TextField 为空。我可以为一个文本字段执行此操作,但我如何编码空的 NSTextFields 出现在 Alert 中?如果 Altert 中的一个 Textfield 为空,则应显示“TextField 1 is empty”。如果字段 1 和 2 为空,则应显示“TextField 1 为空”并在第二行显示“TextField 2 为空”。

这是我的代码:

if ([[TextField1 stringValue] length] == 0) {
    NSAlert* alert = [[NSAlert alloc] init];
    [alert addButtonWithTitle:@"OK"];
    [alert setMessageText:@"Error"];
    [alert setInformativeText:@"TextField 1 is empty"];
    [alert beginSheetModalForWindow:[self.view window] completionHandler:^(NSInteger result) {
        NSLog(@"Success");
    }];
} 

【问题讨论】:

  • NSMutableString *messageText = [[NSMutableString alloc] init]; if ([[TextField1 stringValue] length] == 0) [messageText appendString:@"Textfield 1 is empty"]; if ([[TextField2 stringValue] length] == 0) [messageText appendString:@"Textfield 2 is empty"]; //And so on. if ([messageText length] > 0) //We put at least one message in it) { //Show NGAlert with [alert setInformativeText:messageText]; } ?

标签: objective-c cocoa if-statement nsalert


【解决方案1】:

您可以通过通知自动获取信息。

  • 将标签 1、2、3 分配给文本字段。
  • 将 Interface Builder 中所有文本字段的委托设置为要在其中显示警报的类。
  • 实现这个方法

    - (void)controlTextDidChange:(NSNotification *)aNotification
    {
      NSTextField *field = [aNotification object];
      if ([[field stringValue] length] == 0) {
        NSInteger tag = field.tag;
        NSAlert* alert = [[NSAlert alloc] init];
        [alert addButtonWithTitle:@"OK"];
        [alert setMessageText:@"Error"];
        [alert setInformativeText:[NSString stringWithFormat:@"TextField %ld is empty", tag]];
        [alert beginSheetModalForWindow:[self.view window] completionHandler:^(NSInteger result) {NSLog(@"Success");}];
      }
    }
    

【讨论】:

  • 我没有尝试过,但这不会导致警报立即出现吗?
【解决方案2】:

我会链接 if 语句以获得所需的结果。 设置一个空字符串并一个接一个地检查每个文本字段。如果字符串为空,则将错误行添加到字符串中。附加字符串后不要忘记添加换行符。

我在代码中的话:

NSString* errorMessage = @"";

if ([[TextField1 stringValue] length] == 0) {
    errorMessage = @"TextField 1 is empty.\n";
}

if ([[TextField2 stringValue] length] == 0) {
    errorMessage = [errorMessage stringByAppendingString:@"TextField 2 is empty.\n"];
}   

if ([[TextField3 stringValue] length] == 0) {
    errorMessage = [errorMessage stringByAppendingString:@"TextField 3 is empty."];
}

if (![errorMessage isEqualToString:@""]) {
    NSAlert* alert = [[NSAlert alloc] init];
    [alert addButtonWithTitle:@"OK"];
    [alert setMessageText:@"Error"];
    [alert setInformativeText:errorMessage];
    [alert beginSheetModalForWindow:[self.view window] completionHandler:^(NSInteger result) {
        NSLog(@"Success");
    }];
}

通过这种方式,您可以获得动态输出,具体取决于哪个 NSTextField 为空。

【讨论】:

    猜你喜欢
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    相关资源
    最近更新 更多