zoukankan      html  css  js  c++  java
  • 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];

  • 相关阅读:
    Python编程 从入门到实践-2变量上
    NX二次开发-基于Winform界面对话框与NXOPEN C#交互的开发(对话框嵌套)
    NX二次开发-UFUN获取投影曲线里的曲线UF_CURVE_ask_proj_curves
    NX二次开发-UFUN获取投影曲线里的曲线UF_MODL_ask_proj_curves
    NX二次开发-UFUN创建投影曲线UF_MODL_create_proj_curves
    NX二次开发-NXOPEN C#项目如何设断点调试代码
    NX二次开发-外部开发模式exe(不打开NX进行后台操作)以及封装exe传参调用
    NX二次开发-工程图模板,标题栏,页码,日期,比例,单位,部件名,等自动更新【转载】
    QTreeWidget 遍历所有子节点(QTreeWidgetItem)【转载】
    NX二次开发-使用NXOPEN C#手工搭建开发环境配置
  • 原文地址:https://www.cnblogs.com/PJXWang/p/4921521.html
Copyright © 2011-2022 走看看