Mac开发-如何弹出提示窗口
NSAlert *alert = [[NSAlert alloc] init]; alert.messageText = @"主标题"; alert.informativeText = @“我来了”t;//内容 [alert addButtonWithTitle:@"确定"];//按钮所显示的文案 [alert runModal]; //注意,以上操作需要在主线程中进行,否则会报错
最简单的提示窗
要控制按钮,增加按钮。如图
NSAlert *alert = [[NSAlert alloc] init]; alert.icon = [NSImage imageNamed:@"apple-touch-icon-next"]; alert.alertStyle = NSAlertStyleWarning; // NSAlertStyleWarning = 0, // NSAlertStyleInformational = 1, // NSAlertStyleCritical = 2 [alert addButtonWithTitle:@"确定"]; [alert addButtonWithTitle:@"取消"]; alert.messageText = @"title"; alert.informativeText = @"msg"; alert.showsSuppressionButton = YES; // 方式1 [alert beginSheetModalForWindow:self.view.window completionHandler:^(NSModalResponse returnCode) { if (returnCode == NSAlertFirstButtonReturn) { NSLog(@"确定"); } else if (returnCode == NSAlertSecondButtonReturn) { NSLog(@"取消"); } else { NSLog(@"else"); } }];
其中alertStyle,变换可以切换状态如图
把上面的代码中这一行换成这个状态
alert.alertStyle = NSAlertStyleWarning;
替换
alert.alertStyle = NSAlertStyleCritical;
就是上图的效果了
控制上面按钮的还有方式二
/*! 方式2 NSModalResponse response = [alert runModal]; if (response == NSModalResponseStop) { } if (response == NSModalResponseAbort) { } if (response == NSModalResponseContinue) { } */
使用方式1的时候需要注意的是window不能为nil,不要讲NSAlert
添加在HomeVC的ViewDidLoad中,此时Window还没创建成功。
macOS的另一种弹窗,如图所示
下回再讲