对话框!弹出提示讯息(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方式)