NSAlert 弹出警告提示窗(新)

NSAlert 警告提示窗有以下两种方式展示:


- (IBAction)alertButton:(id)sender {

    

    NSString *title1 =NSLocalizedString(@"Ok"nil);//本地化添加一个Localizable.string文件

    

    NSString *title2 =NSLocalizedString(@"Cancel"nil);

    

    NSString *messagetext =NSLocalizedString(@"messagetext3"nil);

    

    NSString *informativetext =NSLocalizedString(@"Please select the day or the latest"nil);

    

    [self alertSheetFirstBtnTitle:title1 SecondBtnTitle:title2 MessageText:messagetext InformativeText:informativetext];

    

}

//卷帘式对话框alert

-(void)alertSheetFirstBtnTitle:(NSString *)firstname SecondBtnTitle:(NSString *)secondname MessageText:(NSString *)messagetext InformativeText:(NSString *)informativetext{

    

    NSAlert *alert = [[NSAlert allocinit];

    

    [alert addButtonWithTitle:firstname];

    

    [alert addButtonWithTitle:secondname];

    

//    [alert addButtonWithTitle:@"chenglibin1"];//可以添加三个按钮

    

    [alert setMessageText:messagetext];

    

    [alert setInformativeText:informativetext];

    

    [alert setAlertStyle:NSWarningAlertStyle];

    

    [alert beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {

        if (result == NSAlertFirstButtonReturn) {//响应第一个按钮被按下:namefirstname

            //...

            NSLog(@"Ok");

        }

        if (result == NSAlertSecondButtonReturn) {

            NSLog(@"Cancel");

            

        }

        if (result == NSAlertThirdButtonReturn) {

            NSLog(@"chenglibin1");

        }

        

    }];



}


//nsalert 采用Modal Window 的方式展示:

-(void)alertModalFirstBtnTitle:(NSString *)firstname SecondBtnTitle:(NSString *)secondname MessageText:(NSString *)messagetext InformativeText:(NSString *)informativetext{

    

    NSAlert *alert = [[NSAlert allocinit];

    

    [alert addButtonWithTitle:firstname];

    

    [alert addButtonWithTitle:secondname];

    

    //    [alert addButtonWithTitle:@"chenglibin1"];//可以添加三个按钮

    

    [alert setMessageText:messagetext];

    

    [alert setInformativeText:informativetext];

    

    [alert setAlertStyle:NSWarningAlertStyle];

    

    NSUInteger action = [alert runModal];

    //响应window的按钮事件

    if(action == NSAlertFirstButtonReturn)

    {

        NSLog(@"defaultButton clicked!");

    }

    else if(action == NSAlertSecondButtonReturn )

    {

        NSLog(@"alternateButton clicked!");

    }

    else if(action == NSAlertThirdButtonReturn)

    {

        NSLog(@"otherButton clicked!");

    }

    

}

猜你喜欢

转载自blog.csdn.net/qq_27740983/article/details/50328139