使用Alert和Panel

一.使用Alert

一个alert基于NSAlert类,最简单创建一个alert的代码:

var myAlert=NSAlert()
myAlert.runModal()

为了自定义一个alert,可以修改下面的属性:

  • messageText
  • informativeText
  • icon
  • alertStyle
  • showsSuppressionButton
  • suppressionButton

示例代码:

AppDelegate.swift文件

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!


    func applicationDidFinishLaunching(aNotification: NSNotification) {
    }

    func applicationWillTerminate(aNotification: NSNotification) {
    }


    @IBAction func showInfo(sender: AnyObject) {
        let myAlert=NSAlert()
        myAlert.messageText="messageText"
        myAlert.informativeText="informativeText"
        myAlert.alertStyle=NSAlertStyle.CriticalAlertStyle
        myAlert.showsSuppressionButton=true
        myAlert.suppressionButton?.title="下次不再提醒我"
        myAlert.runModal()
    }
}

运行效果:

从alert中获取反馈信息:

一个alert通常只会展示一个OK按钮,但是一个alert可以从用户那里获取反馈信息,通过以下方式:

  • 选择suppression check box
  • 点击除了OK按钮的其它按钮

为了确定用户是否选择了suppression check box,你需要获取suppressionButton.state属性;

你可以通过addButtonWithTitle方法来添加更多的按钮,然后再来确定用户点击了哪个按钮,你需要使用NSAlertFirstButtonReturnNSAlertSecondButtonReturnNSAlertThirdButtonReturn常量,如果alert上的按钮超过三个,你可以使用NSAlertThirdButtonReturn+n来获取按钮。

示例代码:

AppDelegate.swift文件

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!


    func applicationDidFinishLaunching(aNotification: NSNotification) {
    }

    func applicationWillTerminate(aNotification: NSNotification) {
    }


    @IBAction func showInfo(sender: AnyObject) {
        let myAlert=NSAlert()
        myAlert.messageText="messageText"
        myAlert.informativeText="informativeText"
        myAlert.alertStyle=NSAlertStyle.CriticalAlertStyle
        myAlert.showsSuppressionButton=true
        myAlert.suppressionButton?.title="下次不再提醒我"

        myAlert.addButtonWithTitle("第一个")
        myAlert.addButtonWithTitle("第二个")
        myAlert.addButtonWithTitle("第三个")
        myAlert.addButtonWithTitle("第四个")

        let choice=myAlert.runModal()

        switch choice {

        case NSAlertFirstButtonReturn:
            print("第一个")
        case NSAlertSecondButtonReturn:
            print("第二个")
        case NSAlertThirdButtonReturn:
            print("第三个")
        case NSAlertThirdButtonReturn+1:
            print("第四个")
        default:break

        }

        if myAlert.suppressionButton!.state==1{
            print("选中")
        }else{
            print("未选中")
        }


    }
}

效果如下:

以Sheet形式展示Alert

Alert通常以模态对话框的形式展示,另一种展示alert的方式是Sheet:从当前激活窗口的标题栏滚下

示例代码:

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!


    func applicationDidFinishLaunching(aNotification: NSNotification) {
    }

    func applicationWillTerminate(aNotification: NSNotification) {
    }


    @IBAction func showInfo(sender: AnyObject) {
        let myAlert=NSAlert()
        myAlert.messageText="messageText"
        myAlert.informativeText="informativeText"
        myAlert.alertStyle=NSAlertStyle.CriticalAlertStyle
        myAlert.showsSuppressionButton=true
        myAlert.suppressionButton?.title="下次不再提醒我"

        myAlert.addButtonWithTitle("第一个")
        myAlert.addButtonWithTitle("第二个")
        myAlert.addButtonWithTitle("第三个")
        myAlert.addButtonWithTitle("第四个")

        let myCode={ (choice:NSModalResponse) -> Void in

            switch choice {

            case NSAlertFirstButtonReturn:
                print("第一个")
            case NSAlertSecondButtonReturn:
                print("第二个")
            case NSAlertThirdButtonReturn:
                print("第三个")
            case NSAlertThirdButtonReturn+1:
                print("第四个")
            default:break

            }

            if myAlert.suppressionButton!.state==1{
                print("选中")
            }else{
                print("未选中")
            }

        }


        myAlert.beginSheetModalForWindow(window, completionHandler: myCode)
    }
}

效果:

二.使用Panel(面板)

OS X程序可以显示一个Open面板来让用户选择一个文件或者显示一个Save面板来让用户选择一个文件夹来保存文件,分别基于的类是NSOpenPanel和NSSavePanel

1.创建一个Open面板

Open面板让用户选择一个文件来打开,如果用户选择一个文件的话,需要返回一个文件名,属性如下:

  • canChooseFiles
  • canChooseDirectories
  • allowsMultipleSelection
  • URLs

示例代码:

AppDelegate.swift文件

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!


    func applicationDidFinishLaunching(aNotification: NSNotification) {
    }

    func applicationWillTerminate(aNotification: NSNotification) {
    }

    @IBAction func openFile(sender: AnyObject) {

        let myOpen=NSOpenPanel()

        myOpen.canChooseFiles=true

        myOpen.canChooseDirectories=true

        myOpen.allowsMultipleSelection=true

        myOpen.beginWithCompletionHandler{(result) -> Void in
            if result == NSFileHandlingPanelOKButton {
                print(myOpen.URLs)
            }

        }

    }

}

效果图:

2.创建Save面板

一个Save面板目的是让用户选择文件夹,定义一个文件名来存储,如果用户选择一个文件的话,返回一个文件名,属性如下:

  • title
  • prompt
  • URL
  • nameFieldStringValue

AppDelegate.swift文件

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!


    func applicationDidFinishLaunching(aNotification: NSNotification) {
    }

    func applicationWillTerminate(aNotification: NSNotification) {
    }

    @IBAction func openFile(sender: AnyObject) {

        let mySave=NSSavePanel()
        mySave.title="保存文件"
        mySave.prompt="保存我吧"

        mySave.beginWithCompletionHandler{(result) -> Void in

            if result == NSFileHandlingPanelOKButton {
                print(mySave.URL)
                print(mySave.nameFieldStringValue)

            }

            }


        }

    }

效果图如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值