简体   繁体   English

如何显示弹出窗口,通知可可中NSTextField不能为空?

[英]How do I display a popup informing a NSTextField can't be empty in cocoa ?

How do I display a popup informing a NSTextField can't be empty in cocoa ? 如何显示弹出窗口,通知可可中NSTextField不能为空?

If the user click apply and the NSTextField is empty a popup should appear saying the field can't be empty. 如果用户单击应用,并且NSTextField为空,则会出现一个弹出窗口,说明该字段不能为空。

thanks 谢谢

The answer by @beryllium only tells part of the story. @beryllium的回答仅讲述了故事的一部分。

In fact, to properly validate text field input in Cocoa you should be using an NSFormatter attached to your text field cell and then in your NSTextFieldDelegate you should implement the method: control:didFailToFormatString:errorDescription: . 实际上,要正确验证Cocoa中的文本字段输入,您应该使用附加到文本字段单元格的NSFormatter ,然后在NSTextFieldDelegate实现以下方法: control:didFailToFormatString:errorDescription: In this delegate method you can prompt the user to correct their input. 在此委托方法中,您可以提示用户更正其输入。

All you need to do in your NSFormatter subclass is something like this: 您在NSFormatter子类中需要做的事情是这样的:

@implementation RKTextFormatter

- (NSString*)stringForObjectValue:(id)object 
{
    return (NSString*)object;
}

- (BOOL)getObjectValue:(id*)object forString:(NSString*)string errorDescription:(NSString**)error {

    BOOL stringPassesTest = NO;

    //put your test here 

    if(!stringPassesTest)
    {
        //set the error and return NO
        if(error)
            *error = [NSError errorWithDomain:@"YourErrorDomain" code:1 userInfo:[NSDictionary dictionaryWithObject:@"It's a bingo" forKey:NSLocalizedDescriptionKey]];
        return NO;
    }

    //otherwise, just assign the string
    *object = string;
    return YES;
}
@end

You would assign the formatter to your text field like so: 您可以将格式化程序分配给文本字段,如下所示:

RKTextFormatter* formatter = [[RKTextFormatter alloc] init];
[[textField cell] setFormatter:formatter];

And then in your NSTextFieldDelegate you handle any invalid input: 然后在NSTextFieldDelegate处理所有无效输入:

- (BOOL)control:(NSControl *)control didFailToFormatString:(NSString *)string errorDescription:(NSString *)error
{
//display an alert, obviously it would be more useful than this
NSAlert* alert = [NSAlert alertWithMessageText:@"You have failed me for the last time" defaultButton:@"Revise Input" alternateButton:nil otherButton:nil informativeTextWithFormat:@"%@",error];

[alert beginSheetModalForWindow:control.window modalDelegate:nil didEndSelector:NULL contextInfo:NULL];

//if you return NO here, the user's input will not be accepted
//and the field will remain in focus
return NO;
}

If the user click apply and the NSTextField is empty a popup should appear saying the field can't be empty. 如果用户单击应用,并且NSTextField为空,则会出现一个弹出窗口,说明该字段不能为空。

Please, please, do not do this. 请,请不要这样做。 You can be smarter than that. 您可以比这更聪明。

Instead of investing your time in writing an alert dialog to handle the "unexpected" situation, invest it in creating a method that prevents the problem from occurring in the first place: keep the Apply button disabled until a proper value has been entered in the text field . 与其花费时间来编写用于处理“意外”情况的警报对话框,不如花时间去创建一种可以防止问题首先发生的方法: 保持“ Apply按钮处于禁用状态,直到在文本中输入正确的值为止领域

In addition, as @Rob Keniger mentioned, you should consider using NSFormatter s to validate the input to make sure it's of the appropriate kind. 另外,如@Rob Keniger所述,您应该考虑使用NSFormatter来验证输入,以确保输入的类型正确。

Try to use this code: 尝试使用以下代码:

- (IBAction)pushBtn:(id)sender {

    if(self.textfield.stringValue.length == 0){
        NSAlert * alert = [NSAlert alertWithMessageText:@"Error"
                                          defaultButton:@"OK"
                                        alternateButton:nil
                                            otherButton:nil
                              informativeTextWithFormat:@"Enter a text into text field"];

        [alert beginSheetModalForWindow:self.window
                          modalDelegate:self
                         didEndSelector:@selector(alertDidHidden:)
                            contextInfo:nil]; 
        //[alert runModal]; - more simple way
    }
}

-(void)alertDidHidden:(NSAlert *)alert{

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM