您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 使用技巧 >> iOS 7 技巧

Swift 彈窗範例大全:Alert、輸入框與 ActionSheet 一次學會

編輯:iOS 7 技巧

App 裡幾乎一定會用到彈窗:確認刪除、登入輸入、更多操作選單,全都靠它。iOS 8 之後蘋果把 UIAlertView 與 UIActionSheet 整合成統一的 UIAlertController,只要切換 preferredStyle 就能在「中央提示框」與「底部上拉選單」之間轉換。下面這份範例把三種常見型態都做了一遍。

整體結構

做法很直覺:先建立好各個 UIAlertAction(按鈕),再組裝成不同的 UIAlertController,最後用三個按鈕的 tag 決定要跳出哪一個。

import UIKit

class ViewController: UIViewController {
    var alert1: UIAlertController!
    var alert2: UIAlertController!
    var actionSheet: UIAlertController!

    override func viewDidLoad() {
        super.viewDidLoad()

        // 按鈕一:最簡單的 Alert
        let button1 = UIButton.buttonWithType(.System) as! UIButton
        button1.frame = CGRectMake(self.view.frame.width/2 - 200, 50, 400, 50)
        button1.setTitle("最簡單的 Alert", forState: UIControlState.Normal)
        button1.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
        button1.tag = 1

        // 按鈕二:帶文字輸入框的 Alert
        let button2 = UIButton.buttonWithType(.System) as! UIButton
        button2.frame = CGRectMake(self.view.frame.width/2 - 200, 150, 400, 50)
        button2.setTitle("帶文本框的 Alert", forState: UIControlState.Normal)
        button2.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
        button2.tag = 2

        // 按鈕三:上拉選單
        let button3 = UIButton.buttonWithType(.System) as! UIButton
        button3.frame = CGRectMake(self.view.frame.width/2 - 200, 250, 400, 50)
        button3.setTitle("上拉菜單", forState: UIControlState.Normal)
        button3.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
        button3.tag = 3

        // 準備各種 UIAlertAction
        var cancelAction = UIAlertAction(title: "cancel", style: UIAlertActionStyle.Cancel, handler: nil)
        var okAction = UIAlertAction(title: "ok", style: UIAlertActionStyle.Default) {
            (action: UIAlertAction!) -> Void in
            println("you choose ok")
        }
        var saveAction = UIAlertAction(title: "save", style: UIAlertActionStyle.Default) {
            (action: UIAlertAction!) -> Void in
            println("you choose save")
        }
        var deleteAction = UIAlertAction(title: "delete", style: UIAlertActionStyle.Destructive) {
            (action: UIAlertAction!) -> Void in
            println("you choose delete")
        }
        var resetAction = UIAlertAction(title: "reset", style: UIAlertActionStyle.Destructive) {
            (action: UIAlertAction!) -> Void in
            println("you choose reset")
        }

        // 1. 最簡單的 Alert
        alert1 = UIAlertController(title: "simple alert", message: "this is a simple alert",
                                   preferredStyle: UIAlertControllerStyle.Alert)
        alert1.addAction(cancelAction)
        alert1.addAction(resetAction)
        alert1.addAction(okAction)

        // 2. 帶兩個輸入框的登入 Alert
        alert2 = UIAlertController(title: "login alert",
                                   message: "please enter your name and password",
                                   preferredStyle: UIAlertControllerStyle.Alert)
        alert2.addTextFieldWithConfigurationHandler {
            (textField: UITextField!) -> Void in
            textField.placeholder = "name"
        }
        alert2.addTextFieldWithConfigurationHandler {
            (textField: UITextField!) -> Void in
            textField.placeholder = "password"
            textField.secureTextEntry = true   // 密碼欄位遮蔽輸入
        }
        var loginAction = UIAlertAction(title: "login", style: UIAlertActionStyle.Default) {
            (action: UIAlertAction!) -> Void in
            var name = self.alert2.textFields!.first as! UITextField
            var password = self.alert2.textFields!.last as! UITextField
            println("name : \(name.text); password : \(password.text)")
        }
        alert2.addAction(loginAction)

        // 3. 上拉選單(ActionSheet)
        actionSheet = UIAlertController(title: "simple action sheet", message: "action sheet message",
                                        preferredStyle: UIAlertControllerStyle.ActionSheet)
        actionSheet.addAction(cancelAction)
        actionSheet.addAction(deleteAction)
        actionSheet.addAction(saveAction)

        self.view.addSubview(button1)
        self.view.addSubview(button2)
        self.view.addSubview(button3)
    }

    // 依按鈕 tag 決定彈出哪個視窗
    func buttonAction(sender: UIButton) {
        let num = sender.tag
        switch num {
        case 1:
            self.presentViewController(alert1, animated: true, completion: nil)
        case 2:
            self.presentViewController(alert2, animated: true, completion: nil)
        case 3:
            self.presentViewController(actionSheet, animated: true, completion: nil)
        default:
            break
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

重點整理

三個觀念記牢就夠用:第一,UIAlertControllerStyle.Alert 是畫面中央的對話框,.ActionSheet 是從底部滑出的選單;第二,UIAlertActionStyle 分成 Default、Cancel、Destructive 三種,Destructive 會以紅字呈現,適合放「刪除」、「重設」這類破壞性操作;第三,輸入框透過 addTextFieldWithConfigurationHandler 加入,讀值時從 textFields 陣列取出,記得對密碼欄位設 secureTextEntry = true。

另外提醒一個介面習慣:ActionSheet 在 iPad 上需要用 popover 的方式呈現,且同一個畫面同時只會有一個彈窗存在,若在彈窗未關閉前又呼叫 presentViewController,系統會靜默失敗。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved