【发布时间】: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