Swift UIAlertView/UIActionSheet

來源:互聯網
上載者:User

標籤:

UIAlertView


警告顯示使用者向使用者顯示一個簡潔和資訊的警告資訊。警告視圖傳達了一個應用程式或裝置的重要訊息,打斷了使用者,要求他們停止他們正在做的選擇行動或解僱警報。例如,iOS使用警報,警告使用者電池電量不足,所以他們可以在他們的工作被中斷串連電來源配接器。警示檢視出現在頂部的應用程式的內容,必須手動解除由使用者才可以與應用程式恢複作用。

UIAlertView的使用1.樣式1


<span style="font-size:14px;">// Default樣式 建立控制項方式1    func initUIAlertView1(){        let alertView1 = UIAlertView.init(title: "樣式1", message: "測試", delegate: self, cancelButtonTitle: "取消", otherButtonTitles: "確定","按鈕3") // Available in iOS 2.0 and later.Deprecated in iOS 9.0.        alertView1.show()    }</span>

2.樣式2


func initUIAlertView2(){        let alertView2 = UIAlertView.init()        alertView2.title = "樣式2"        alertView2.message = "測試"        alertView2.addButtonWithTitle("取消") // 先添加 "取消" 按鈕,它的index = 0        alertView2.addButtonWithTitle("確定") // index = 1        alertView2.delegate = self        alertView2.show()    }

上面樣式1和樣式2是一樣的,只是兩種不同的建立控制項的方式,且樣式1比樣式2多了一個按鈕.

3.樣式3


func initUIAlertView3(){        let alertView3 = UIAlertView.init()        alertView3.alertViewStyle = UIAlertViewStyle.SecureTextInput // 密文        alertView3.title = "SecureTextInput"        alertView3.addButtonWithTitle("確定")        alertView3.delegate = self        alertView3.show()    }

4.樣式4


func initUIAlertView4(){        let alertView4 = UIAlertView.init()        alertView4.alertViewStyle = UIAlertViewStyle.PlainTextInput  // 非密文        alertView4.title = "PlainTextInput"        alertView4.addButtonWithTitle("確定")        alertView4.delegate = self        alertView4.show()    }

樣式3和樣式4,基本也是一樣,知識視圖中間的TextFiled一個是密文一個不是.

5.樣式5


    func initUIAlertView5(){        let alertView5 = UIAlertView.init()        alertView5.alertViewStyle = UIAlertViewStyle.LoginAndPasswordInput  // 登入使用者名稱和密碼輸入樣式        alertView5.title = "LoginAndPasswordInput"        alertView5.addButtonWithTitle("確定")        alertView5.delegate = self        alertView5.show()    }

UIAlertViewDelegate代理,一般使用只需要實現有一個方法就可以了.

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {        print("title = \(alertView.buttonTitleAtIndex(buttonIndex)) buttonIndex = \(buttonIndex)") // 列印按鈕的title和index        if alertView.alertViewStyle == UIAlertViewStyle.LoginAndPasswordInput {            print("\(alertView.textFieldAtIndex(0)?.text)") // 列印輸入的內容            print("\(alertView.textFieldAtIndex(1)?.text)") // 列印輸入的內容        }        else{            print("\(alertView.textFieldAtIndex(0)?.text)") // 列印輸入的內容        }    }


UIActionSheetUIActionSheet的使用和UIAlertView基本一樣,只是兩種不同的控制項,和顯示風格.


UIActionSheet的使用

1.樣式1


這種是系統預設樣式

<span style="font-size:14px;"> func initUIActionSheet1(){        let actionSheet1 = UIActionSheet.init(title: "UIActionSheet", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: "確定")// Available in iOS 2.0 and later.Deprecated in iOS 8.3.        actionSheet1.showInView(self.view)    }</span>

2.樣式2


func initUIActionSheet2(){        let actionSheet2 = UIActionSheet.init()        actionSheet2.title = "我是title"        actionSheet2.addButtonWithTitle("確定")        actionSheet2.addButtonWithTitle("取消")        actionSheet2.delegate = self        actionSheet2.showInView(self.view)    }

UIActionSheetDelegate
    func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {        print("title = \(actionSheet.buttonTitleAtIndex(buttonIndex)) buttonIndex = \(buttonIndex)") // 列印按鈕的title和index    }

Demo介面

整個介面是做了7個按鈕來觸發這個兩個控制項的7種樣式



<span style="font-size:14px;">@IBAction func btnClick(sender: UIButton) {        let tag = sender.tag        // 列印按鈕tag和標題        print("tag = \(tag) title = \(sender.titleLabel?.text)")        switch tag {        case 1:            self.initUIAlertView1()            break        case 2:            self.initUIAlertView2()            break        case 3:            self.initUIAlertView3()            break        case 4:            self.initUIAlertView4()            break        case 5:            self.initUIAlertView5()            break                    default: break                    }    }</span>

<span style="font-size:14px;">@IBAction func btnClick2(sender: UIButton) {        let tag = sender.tag        // 列印按鈕tag和標題        print("tag = \(tag) title = \(sender.titleLabel?.text)")        switch tag {        case 1:            self.initUIActionSheet1()            break        case 2:            self.initUIActionSheet2()            break        default: break        }    }</span>


Swift UIAlertView/UIActionSheet

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.