NSAlert 弹出警告提示窗(新)

本文介绍了如何在Mac应用中使用NSAlert警告提示窗。通过两种不同的方式展示警告窗:sheet模态窗口和模态对话框。代码示例展示了如何设置按钮、消息文本和信息文本,并响应用户操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

NSAlert 警告提示窗有以下两种方式展示:


- (IBAction)alertButton:(id)sender {

    

    NSString *title1 =NSLocalizedString(@"Ok"nil);//本地化添加一个Localizable.string文件

    

    NSString *title2 =NSLocalizedString(@"Cancel"nil);

    

    NSString *messagetext =NSLocalizedString(@"messagetext3"nil);

    

    NSString *informativetext =NSLocalizedString(@"Please select the day or the latest"nil);

    

    [self alertSheetFirstBtnTitle:title1 SecondBtnTitle:title2 MessageText:messagetext InformativeText:informativetext];

    

}

//卷帘式对话框alert

-(void)alertSheetFirstBtnTitle:(NSString *)firstname SecondBtnTitle:(NSString *)secondname MessageText:(NSString *)messagetext InformativeText:(NSString *)informativetext{

    

    NSAlert *alert = [[NSAlert allocinit];

    

    [alert addButtonWithTitle:firstname];

    

    [alert addButtonWithTitle:secondname];

    

//    [alert addButtonWithTitle:@"chenglibin1"];//可以添加三个按钮

    

    [alert setMessageText:messagetext];

    

    [alert setInformativeText:informativetext];

    

    [alert setAlertStyle:NSWarningAlertStyle];

    

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

        if (result == NSAlertFirstButtonReturn) {//响应第一个按钮被按下:namefirstname

            //...

            NSLog(@"Ok");

        }

        if (result == NSAlertSecondButtonReturn) {

            NSLog(@"Cancel");

            

        }

        if (result == NSAlertThirdButtonReturn) {

            NSLog(@"chenglibin1");

        }

        

    }];



}


//nsalert 采用Modal Window 的方式展示:

-(void)alertModalFirstBtnTitle:(NSString *)firstname SecondBtnTitle:(NSString *)secondname MessageText:(NSString *)messagetext InformativeText:(NSString *)informativetext{

    

    NSAlert *alert = [[NSAlert allocinit];

    

    [alert addButtonWithTitle:firstname];

    

    [alert addButtonWithTitle:secondname];

    

    //    [alert addButtonWithTitle:@"chenglibin1"];//可以添加三个按钮

    

    [alert setMessageText:messagetext];

    

    [alert setInformativeText:informativetext];

    

    [alert setAlertStyle:NSWarningAlertStyle];

    

    NSUInteger action = [alert runModal];

    //响应window的按钮事件

    if(action == NSAlertFirstButtonReturn)

    {

        NSLog(@"defaultButton clicked!");

    }

    else if(action == NSAlertSecondButtonReturn )

    {

        NSLog(@"alternateButton clicked!");

    }

    else if(action == NSAlertThirdButtonReturn)

    {

        NSLog(@"otherButton clicked!");

    }

    

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值