第十五章: 使用 Alert Panels

本文介绍了如何在Cocoa应用中使用NSRunAlertPanel函数生成警告面板,包括其参数、模式和如何通过面板与用户交互进行确认操作。

有时候,我们可能想要通过一个Alert Panel来给用户一些警告信息. Alert panel很容易生成,在cocoa中,大部分的东西都是面向对象的,不过显示一个alert panel却是通过一个C函数来实现: NSRunAlertPanel() . 下面是函数声明:
int NSRunAlertPanel(NSString *title, NSString *msg,
       NSString *defaultButton, NSString *alternateButton,
       NSString *otherButton, ...);

下面的代码可以生成图15.1的Alert panel
int choice = NSRunAlertPanel(@"Fido", @"Rover",
                             @"Rex", @"Spot", @"Fluffy");

注意到panel上的icon就是程序的icon.第二个和第三个button是可选的. 使用nil参数来代替标题,button就不会显示出来

函数NSRunAlertPanel()会返回一个int值来指示用户点击了那个button. 这个值会是全局常量中的一个: NSAlertDefaultReturn, NSAlertAlternateReturn, 和NSAlertOtherReturn.

NSRunAlertPanel()函数有多个参数输入, 其中第二个参数是一个string,和printf一些可以使用一些代替符号. 从otherButton参数后面开始提供的参数值就是提供给第二个参数的值.所以以下代码显示图15.2 Alert panel
int choice = NSRunAlertPanel(@"Fido", @"Rover is %d",
                             @"Rex", @"Spot", nil, 8);

Alert panel运行在 modally模式. 在alert panel退出消失前,程序中的其他windows将不会接受到任何的事件.

Alert也可以运行在sheet模式.sheet是莫个window上下拉出来的一个window. 在sheet消失前,它关联的那个window接受不到任何按键和鼠标事件


-- 让用户确认删除 --

当用户点击Delete button, 在删除记录前,将会以一个sheet方式弹出一个Alert panel如图15.3


为了实现这个功能,首先打开MyDocument.nib,选择table view,打开Inspector. 设置容许用户多选.如图15.4

接下来,我们设置Delete button发送一个消息给MyDocument来让用户确认删除动作.如果用户确认要删除,那么MyDocument将发送removeEmployee:消息给array controller来删除所选择的Person

在XCode中, 打开MyDocument.h文件,添加Delete button将要触发的方法
- (IBAction)removeEmployee:(id)sender;

在MyDocument.m中实现removeEmployeeL方法. 我们将会先实现一个sheet alert
- (IBAction)removeEmployee:(id)sender
{
   NSArray *selectedPeople = [employeeController selectedObjects];
   NSAlert *alert = [NSAlert alertWithMessageText:@"Delete?"
            defaultButton:@"Delete"
          alternateButton:@"Cancel"
              otherButton:nil
informativeTextWithFormat:@"Do you really want to delete %d people?",
                            [selectedPeople count]];
   NSLog(@"Starting alert sheet");
   [alert beginSheetModalForWindow:[tableView window]
                     modalDelegate:self
                    didEndSelector:@selector(alertEnded:code:context:)
                       contextInfo:NULL];
}

这个方法会生成一个sheet,当用户点击sheet alert中的一个button,将会给document对象发送 alerEnded:code:context:消息 [为什么会给document 对象发送该消息呢?那是因为我们在beginSheetModalForWindow方法中将modalDelegate参数设置为了self]
- (void)alertEnded:(NSAlert *)alert
              code:(int)choice
           context:(void *)v
{
    NSLog(@"Alert sheet ended");
    // If the user chose "Delete", tell the array controller to
    // delete the people
    if (choice == NSAlertDefaultReturn) {
        // The argument to remove: is ignored
        // The array controller will delete the selected objects
        [employeeController remove:nil];
    }
}

打开MyDocument.nib. Control-drag Delete button到File's Owner.让File's Owner成为新的target [MyDocument.nib file''s Owner占位符,将就是设置成MyDocument对象了]. 设置action为removeEmployee: 如图15.5


编译运行程序.


-- 挑战 --
给Alert sheet添加另外一个button: Keep. 当点击这个button时,将会把所选的employees的raises设置为0.

转载于:https://www.cnblogs.com/pengyingh/articles/2350621.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值