Cocoa Programming for Mac OS X 第十五章(Using Alert Panels)摘录

Using Alert Panels

Occasionally, you will want to warn the user about something by means of an Alert panel. Alert panels are easy to create. Most things in Cocoa are object oriented, but showing a modal Alert panel is typically done with a C function:NSRunAlertPanel(). Here is the declaration:

int NSRunAlertPanel(NSString *title, NSString *msg,
       NSString *defaultButton, NSString *alternateButton,
       NSString *otherButton, ...);

The code

int choice = NSRunAlertPanel(@"Fido", @"Rover",
                             @"Rex", @"Spot", @"Fluffy");

would result in the Alert panel shown in Figure 15.1.

Figure 15.1. Example Alert Panel


Note that the icon on the panel will be the icon for the responsible application. The second and third buttons are optional. To prevent a button from appearing, replace its label with nil.

The NSRunAlertPanel() function returns an int that indicates which button the user clicked. There are global variables for these constants: NSAlertDefaultReturnNSAlertAlternateReturn, and NSAlertOtherReturn.

Note that NSRunAlertPanel() takes a variable number of arguments. The second string may include printf-like tokens. Values supplied after the otherButton label will be substituted in. Thus, the code

int choice = NSRunAlertPanel(@"Fido", @"Rover is %d",
                             @"Rex", @"Spot", nil, 8);

would result in the Alert panel shown in Figure 15.2.

Figure 15.2. Another Example Alert Panel


Alert panels run modally; that is, other windows in the application don't receive events until the Alert panel has been dismissed.

Alerts can also be run as a sheet. A sheet is a window that drops down in front of another window. Until the sheet is dismissed, no keyboard or mouse events will be dispatched to the obscured window.

Make the User Confirm the Deletion

If the user clicks the Delete button, an Alert panel should appear as a sheet before the records are deleted (Figure 15.3).

Figure 15.3. Completed Application


To enable this behavior, open MyDocument.nib, select the table view, and open the Inspector. Allow the user to make multiple selections (Figure 15.4).

Figure 15.4. Inspect TableView


You now want the Delete button to send to MyDocument a message that will ask the user to confirm the deletion. If the user confirms this choice, MyDocument will send the removeEmployee: message to the array controller to remove the selected Personobjects.

In Xcode, open the MyDocument.h file and add the method that will be triggered by the Delete button:

- (IBAction)removeEmployee:(id)sender;

In MyDocument.m, implement the removeEmployee: method, which will start the Alert panel as a sheet:

- (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];
}

This method will start the sheet. When the user clicks a button, the document object will get sent thealertEnded:code:context: message:

- (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];
    }
}

Open MyDocument.nib. Control-drag from the Delete button to the File's Owner icon to make File's Owner be the new target. Set the action to removeEmployee: (Figure 15.15.

Figure 15.5. Change target/action of Delete Button


Build and run your application.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值