• NSAlert


    NSAlert用于弹出一个确认对话框,在程序中被广泛地使用。常见的场景是用户删除数据,会弹出对话框给用户确认,免得用户不小心导致了误操作。

    NSAlert可以采用Modal Window的方式展示

     

    //采用Modal Window的方式展示

    - (IBAction)ShowNSAlertWindow:(id)sender

    {

     

        NSTextView *accessory = [[NSTextView alloc] initWithFrame:NSMakeRect(0,0,200,15)];

        NSFont *font = [NSFont systemFontOfSize:[NSFont systemFontSize]];

        NSDictionary *textAttributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];

        [accessory insertText:[[NSAttributedString alloc] initWithString:@"Text in accessory view."                                                                        attributes:textAttributes]];

        [accessory setEditable:NO];

        [accessory setDrawsBackground:NO];

       

        NSAlert *alert = [[NSAlert alloc] init];

        [alert setAlertStyle:NSWarningAlertStyle];

        [alert setMessageText:@"message text"];

        [alert setInformativeText:@"informative text"];

        [alert setHelpAnchor:@"help anchor"];

        //[alert setShowsSuppressionButton:YES];

        [alert addButtonWithTitle:@"stop"];

        [alert addButtonWithTitle:@"abort" ];

        [alert addButtonWithTitle:@"Cancel"];

        [alert setAccessoryView:accessory];

       

        //__bridge_retained for arc

        NSUInteger result = [alert runModal];

       if (result == NSAlertFirstButtonReturn) {

                NSLog(@"Stop");

            }

            else if (result == NSAlertSecondButtonReturn){

                NSLog(@"abort");

            }

            else if (result == NSAlertThirdButtonReturn){

                NSLog(@"cancel");

            }

           

    }

    NSAlert也可以采用Sheet的方式展示

    //采用Sheet的方式展示

    - (IBAction)ShowNSAlertSheet:(id)sender

    {

      NSTextView *accessory = [[NSTextView alloc] initWithFrame:NSMakeRect(0,0,200,15)];

        NSFont *font = [NSFont systemFontOfSize:[NSFont systemFontSize]];

        NSDictionary *textAttributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];

        [accessory insertText:[[NSAttributedString alloc] initWithString:@"Text in accessory view."                                                                        attributes:textAttributes]];

        [accessory setEditable:NO];

        [accessory setDrawsBackground:NO];

       

        NSAlert *alert = [[NSAlert alloc] init];

        [alert setAlertStyle:NSWarningAlertStyle];

        [alert setMessageText:@"message text"];

        [alert setInformativeText:@"informative text"];

        [alert setHelpAnchor:@"help anchor"];

        //[alert setShowsSuppressionButton:YES];

        [alert addButtonWithTitle:@"stop"];

        [alert addButtonWithTitle:@"abort" ];

        [alert addButtonWithTitle:@"Cancel"];

        [alert setAccessoryView:accessory];

      

        //__bridge_retained for arc

        [alert beginSheetModalForWindow:[self window] completionHandler:^(NSInteger result){

            if (result == NSAlertFirstButtonReturn) {

                NSLog(@"Stop");

            }

            else if (result == NSAlertSecondButtonReturn){

                NSLog(@"abort");

            }

            else if (result == NSAlertThirdButtonReturn){

                NSLog(@"cancel");

            }

           

    }];

     

     

    Instance Attributes:

     

      -setAlertStyle:

    @property NSAlertStyle alertStyle;

    enum {

       NSWarningAlertStyle = 0, //警告

       NSInformationalAlertStyle = 1, //信息

       NSCriticalAlertStyle = 2 //错误

    };

    typedef NSUInteger NSAlertStyle;

    Message text   -setMessageText:

    @property(copy) NSString *messageText

    Informative text   -setInformativeText:

    @property(copy) NSString *informativeText

    Icon    -setIcon:

    @property(strong) NSImage *icon

    Help     -setHelpAnchor:  -setShowsHelp:

    @property(copy) NSString *helpAnchor

    Response buttons   -addButtonWithTitle:

    //从右至左按钮的id是NSAlertFirstButtonReturn, NSAlertSecondButtonReturn, NSAlertThirdButtonReturn 再往后是NSAlertThirdButtonReturn +n

    默认选中NSAlertFirstButtonReturn, 任何标为“Cancel”与快捷键ESC相连接

    “Don’t Save”不是第一个按钮时,默认和快捷键Command-D连接

    NSButton中- (void)setKeyEquivalent:(NSString *)charCode设置快捷键,-setTag:设置button的返回id

    Suppression checkbox   -setShowsSuppressionButton:

    NSString *exampleAlertSuppress = @"ExampleAlertSuppress";

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if ([defaults boolForKey:exampleAlertSuppress]) {

    NSLog(@"ExampleAlert suppressed");

    }

    else {

    NSAlert *alert = [[NSAlert alloc] init];

    [alert setMessageText:@"Message text."];

    [alert setInformativeText:@"Informative text."];

    [alert setShowsSuppressionButton:YES];

    [alert runModal];

    if ([[alert suppressionButton] state] == NSOnState) {

    //Suppress this alert from now on.

    [defaults setBool:YES forKey:exampleAlertSuppress];

    }

    [alert release];

                }

    Accessory view   -setAccesssoryView:

    @property(strong) NSView *accessoryView

                NSTextView *accessory = [[NSTextView alloc] initWithFrame:NSMakeRect(0,0,200,15)];

                NSFont *font = [NSFont systemFontOfSize:[NSFont systemFontSize]];

                NSDictionary *textAttributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];

                [accessory insertText:[[NSAttributedString alloc] initWithString:@"Text in accessory view."                                                                              attributes:textAttributes]];

                [accessory setEditable:NO];

                [accessory setDrawsBackground:NO];

                 

                NSAlert *alert = [[NSAlert alloc] init];

                [alert setMessageText:@"Message text."];

                [alert setInformativeText:@"Informative text."];

                [alert setAccessoryView:accessory];

                [alert runModal];

                            [alert release];

  • 相关阅读:
    mysql 获取字符串的长度
    mysql 字符类以及重复元字符
    mysql 字段拼接
    mysql 去除字符串中的空格
    mysql 正则表达式
    mysql 选出前五个元素
    mysql regexp 表达式
    mysql 选择所有同学名字
    mysql 获取数学成绩最高以及最低的同学
    mysql 获取单个科目的平均分
  • 原文地址:https://www.cnblogs.com/PJXWang/p/4921521.html
Copyright © 2020-2023  润新知