對話框!彈出提示訊息(Alert)
跳出式對話框在每個平台語言都是很普遍的功能,在Mac上也不意外的提供這項功能,但依照外觀分為2種:
獨立視窗
執行後會以獨立視窗方式跳出顯示,顯示的位置大都在螢幕正中央,非程式視窗的中央。
程式內容:
//-----------start----------- NSAlert *alert = [[NSAlert alloc] init];//右邊開始算起 [alert addButtonWithTitle:@"2012"];//右一 [alert addButtonWithTitle:@"2013"];//右二 [alert addButtonWithTitle:@"2014"];//右三 [alert addButtonWithTitle:@"2015"];//右四 [alert setMessageText:@"選擇您的答案"];//標頭 [alert setInformativeText:@"現在的年份?"];//內容 [alert setAlertStyle:NSCriticalAlertStyle]; NSInteger choice = [alert runModal]; NSLog(@"Dialog:%ld",(long)choice); //------------end------------
這算是最基本的功能,可以使用addButtonWithTitle
這個metod增加Button,官方建議是最多增加三個,再利用 runModal
顯示對話框,其後的返回值就是你選擇哪個Button的值,排列從右開始,像程式註解中的右一、右二、右三、右四,其值內從右一值1000間隔1累加:
右一:1000 右二:1001 右三:1002 右四:1003 : : :
如果你只有三個以內的Button,可以使用系統建立的名稱:
//-----------start----------- enum { NSAlertFirstButtonReturn = 1000, NSAlertSecondButtonReturn = 1001, NSAlertThirdButtonReturn = 1002 }; //------------end------------
所以程式可以再加上判斷:
//-----------start----------- NSInteger choice = [alert runModal]; NSLog(@"Dialog:%ld",(long)choice); if (choice == NSAlertFirstButtonReturn) { //右一按鍵要做的事 }else if (choice == NSAlertSecondButtonReturn){ //右二按鍵要做的事 } //------------end------------
合併視窗
執行後會與原本視窗結合,並且顯示於視窗標題列的下方,對於操作上較獨立視窗方式與應用程式本身有緊密的結合。
使用這個視窗的樣式必需要指定delegate的method,並且也要指定上層windos,所以程式內容:
//-----------start----------- NSAlert *alert = [[NSAlert alloc] init]; [alert addButtonWithTitle:@"2012"]; [alert addButtonWithTitle:@"2013"]; [alert addButtonWithTitle:@"2014"]; [alert addButtonWithTitle:@"2015"]; [alert setMessageText:@"選擇您的答案"]; [alert setInformativeText:@"現在的年份?"]; [alert setAlertStyle:NSInformationalAlertStyle]; //[self window] 取得上層視窗 [alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:@selector(alertEnded:code:context:) contextInfo:nil]; } //------------end------------
指定操作完後執行的method為alertEnded:code:context:
,程式如下:
//-----------start----------- -(void)alertEnded:(NSAlert *)alert code:(int)choice context:(void *)v{ //choice為選擇按鍵的回傳值(1000~1000+ButtonN) NSLog(@"Dialog1:%ld",(long)choice); } //------------end------------
OS X 10.9
10.9之後提供Block(GCD)方式,所以就可以更省事不需要指定method:
//-----------start----------- NSAlert *alert = [[NSAlert alloc] init]; [alert addButtonWithTitle:@"2012"]; [alert addButtonWithTitle:@"2013"]; [alert addButtonWithTitle:@"2014"]; [alert addButtonWithTitle:@"2015"]; [alert setMessageText:@"選擇您的答案"]; [alert setInformativeText:@"現在的年份?"]; [alert setAlertStyle:NSWarningAlertStyle]; //[self window] 取得上層視窗 [alert beginSheetModalForWindow:[self window] completionHandler:^(NSModalResponse returnCode) { NSLog(@"Dialog2:%ld",(long)returnCode); }]; //------------end------------
NSAlert其他method
程式中可以看到幾個method,大略解釋一下:
- setAlertStyle 設定對話框顯示時,右邊的icon風格(NSWarningAlertStyle、NSInformationalAlertStyle、NSCriticalAlertStyle)
- setMessageText 對話框的標題內容
- setInformativeText 對話框的說明內容
範例程式:
依照先前解說的分為三個按鍵提供展示,從右開始:
- Dialog 一般使用方式
- Dialog1 與上層視窗結合(需Delegate配合)
Dialog2 10.9以後提供與上層視窗結合的方法(Block方式)