警报是确认用户想要删除文档。
我想要“删除”按钮,然后运行删除功能和“取消”只是为了关闭警报。
我如何在Swift写这个?
谢谢
NSAlert *alert = [[[NSAlert alloc] init] autorelease]; [alert addbuttonWithTitle:@"Delete"]; [alert addbuttonWithTitle:@"Cancel"]; [alert setMessageText:@"Delete the document?"]; [alert setinformativeText:@"Are you sure you would like to delete the document?"]; [alert setAlertStyle:NSWarningalertStyle]; [alert beginSheetModalForWindow:[self window] modalDelegate:self dIDEndSelector:@selector(alertDIDEnd:returnCode:contextInfo:) contextInfo:nil];beginSheetModalForWindow:在OS X 10.10 Yosemite中不推荐使用modalDelegate。
斯威夫特2
func dialogoKCancel(question: String,text: String) -> Bool { let myPopup: NSAlert = NSAlert() myPopup.messageText = question myPopup.informativeText = text myPopup.alertStyle = NSAlertStyle.WarningalertStyle myPopup.addbuttonWithTitle("OK") myPopup.addbuttonWithTitle("Cancel") let res = myPopup.runModal() if res == NSAlertFirstbuttonReturn { return true } return false}let answer = dialogoKCancel("Ok?",text: "Choose your answer.")
这将根据用户的选择返回true或false。
NSAlertFirstbuttonReturn表示添加到对话框的第一个按钮,这里是“OK”。
Swift 3
func dialogoKCancel(question: String,text: String) -> Bool { let myPopup: NSAlert = NSAlert() myPopup.messageText = question myPopup.informativeText = text myPopup.alertStyle = NSAlertStyle.warning myPopup.addbutton(withTitle: "OK") myPopup.addbutton(withTitle: "Cancel") return myPopup.runModal() == NSAlertFirstbuttonReturn}let answer = dialogoKCancel(question: "Ok?",text: "Choose your answer.")总结
以上是内存溢出为你收集整理的用Swift创建NSAlert全部内容,希望文章能够帮你解决用Swift创建NSAlert所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)