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

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

概述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 mod @H_301_0@ @H_301_0@ 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


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

[View full size image]


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

[View full size image]


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

[VIEw full size image]


Build and run your application.

@H_301_0@ 总结

以上是内存溢出为你收集整理的Cocoa Programming for Mac OS X 第十五章(Using Alert Panels)摘录全部内容,希望文章能够帮你解决Cocoa Programming for Mac OS X 第十五章(Using Alert Panels)摘录所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1066397.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存