Mac開發-如何彈出提示窗口
NSAlert *alert = [[NSAlert alloc] init]; alert.messageText = @"主標題"; alert.informativeText = @“我來了”t;//內容 [alert addButtonWithTitle:@"確定"];//按鈕所顯示的文案 [alert runModal]; //注意,以上操作需要在主線程中進行,否則會報錯
最簡單的提示窗
要控制按鈕,增加按鈕。如圖
NSAlert *alert = [[NSAlert alloc] init]; alert.alertStyle = NSAlertStyleWarning; [alert addButtonWithTitle:TBLocalizedString(@"確定")];
[alert addButtonWithTitle:TBLocalizedString(@"取消")];
alert.messageText = TBLocalizedString(@"提示"); alert.informativeText = TBLocalizedString(@"訂單指令已提交"); // 方式1 [alert beginSheetModalForWindow:self.view.window completionHandler:^(NSModalResponse returnCode) { if (returnCode == NSAlertFirstButtonReturn) { NSLog(@"確定"); } else if (returnCode == NSAlertSecondButtonReturn) { NSLog(@"取消"); } else { NSLog(@"else"); } }]; /*! 方式2 NSModalResponse response = [alert runModal]; if (response == NSModalResponseStop) { } if (response == NSModalResponseAbort) { } if (response == NSModalResponseContinue) { } */
其中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的另一種彈窗,如圖所示
下回再講