Mac OSX App NSAlert的使用
iOS App UIAlert 的使用方法那就很简单了,也是我们经常使用的一张UI控件。
今天写了个Mac App 的,用到了NSAlert,但是没有想到 它的使用比想象中的麻烦。下面简单给个例子:
我写了Button 然后 弹出来Alert view 。
然后我点击删除按钮 情况 面板的内容,清空前给用户一个提示:
具体代码:
- (IBAction)downbutton:(id)sender {
NSAlert *alert = [NSAlert new];
[alert addButtonWithTitle:@"确定"];
[alert addButtonWithTitle:@"取消"];
[alert setMessageText:@"确定删除输入文本?"];
[alert setInformativeText:@"如果确定删除,删除的文本不能再找回!"];
[alert setAlertStyle:NSWarningAlertStyle];
[alert beginSheetModalForWindow:[self.view window] completionHandler:^(NSModalResponse returnCode) {
if(returnCode == NSAlertFirstButtonReturn){
NSLog(@"确定");
[self.inputview setString:@""];
}else if(returnCode == NSAlertSecondButtonReturn){
NSLog(@"删除");
}
}];
}
NSAlert 以前的使用方法为:
- (void)beginSheetModalForWindow:(NSWindow *)window modalDelegate:(id)delegate didEndSelector:(SEL)didEndSelector contextInfo:(void *)contextInfo
现在使用新方法在Block里面直接连接处理Button事件:
- (void)beginSheetModalForWindow:(NSWindow *)sheetWindow completionHandler:(void (^)(NSModalResponse returnCode))handler NS_AVAILABLE_MAC(10_9);
可以通过判断returnCode来区别不同的Button。
注意点
- NSAlert View 的Button 是直接加的,例如:[alert addButtonWithTitle:@”确定”];
- beginSheetModalForWindow 的 NSWindow