4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Swift】UIAlertControllerのActionをクロージャで処理したくない まとめ

Posted at

概要

【Swift】UIAlertControllerのActionをクロージャで処理したくない
【Swift】UIAlertControllerのActionをクロージャで処理したくない2

というわけで目的としては
・UIAlertControllerを利用を簡潔にしたい
・連続でAlertを呼び出し可能にしたい。(Alertで質問への回答など)

です。
とりあえず動くことまで確認出来たので、UIAlertControllerについては以上とします。

共通ロジック

Common/Alert.swift
import UIKit

protocol AlertDelegate{
    func alertClose(title:String)
}

/*
利用時
1.AlertDelegateプロトコルを適合する。
2.title,messageを設定してdefaultActionArrayに設定したいラベル(String)をappendする。
3.preferredStyleにAlertかActionSheetが設定可能
4.isHaveCancelでキャンセルが必要か設定可能
5.showAlertControllerをCallする

TODO
・メンバ変数はprivateにすべき
・isHaveCancelって名前おかしくないか?
・もっとスマートにかけないか

*/
class Alert{
    
    //イニシャライザ
    convenience init(){
        self.init(title: "default",message: "default")
    }
    
    convenience init(title:String,message:String){
        self.init(title: title,message: message,preferredStyle: UIAlertControllerStyle.Alert)
    }
    
    init(title:String,message:String,preferredStyle:UIAlertControllerStyle){
        self.title = title
        self.message = message
        self.preferredStyle = preferredStyle
    }
    
    var delegate:AlertDelegate!
    
    //Alert表示設定項目
    var isHaveCancel:Bool = true;
    var title:String = "";
    var message:String = "";
    var preferredStyle:UIAlertControllerStyle = UIAlertControllerStyle.Alert
    
    var defaultActionArray:[String] = Array()
    
    func showAlertController(view:UIViewController){
        var alert:UIAlertController = UIAlertController(title:title,
            message: message,
            preferredStyle: preferredStyle)
        
        //キャンセルアクションの追加
        if(isHaveCancel){
            let cancelAction:UIAlertAction = UIAlertAction(title: "キャンセル",
                style: UIAlertActionStyle.Cancel,
                handler:{
                    (action:UIAlertAction!) -> Void in
                    self.delegate.alertClose("キャンセル")
            })
            
            alert.addAction(cancelAction)
        }
        
        //defaultアクションの追加
        for defaultTitle in defaultActionArray{
            let defaultAction:UIAlertAction = UIAlertAction(title: defaultTitle,
                style: UIAlertActionStyle.Default,
                handler:{
                    (action:UIAlertAction!) -> Void in
                    self.delegate.alertClose(defaultTitle)
            })
            alert.addAction(defaultAction)
        }
        
        view.presentViewController(alert, animated: true, completion: nil)
    }
}

利用クラス

CallFrom.swift
import UIKit

class ViewController: UIViewController,AlertDelegate {

    // ボタンイベント.
    func onClickMyButton(sender: UIButton){
        self.alertCallFirst()
    }

    var alertAction:(String) -> () = {(title:String) -> () in
        println("not impliment")
    }
    
    func alertClose(title:String) {
        self.alertAction(title)
    }
    
    private func alertCallFirst(){
        
        //Actionの表示名とわかりやすい名称をつける
        let ACTION_VAL1 = "VAL1"
        let ACTION_VAL2 = "VAL2"
        let ACTION_VAL3 = "VAL3"
        
        var alert = Alert(title: "",message: "",preferredStyle: UIAlertControllerStyle.ActionSheet)
        alert.delegate = self
        alert.isHaveCancel = false

        alert.defaultActionArray.append(ACTION_VAL1)
        alert.defaultActionArray.append(ACTION_VAL2)
        alert.defaultActionArray.append(ACTION_VAL3)
        
        self.alertAction = {(title:String) -> () in
            switch title{
            case ACTION_VAL1:
                break
            case ACTION_VAL2:
                self.alertCallSecond()
                break
            case ACTION_VAL3:
                break;
            default:
                break;
            }
        }
        
        alert.showAlertController(self)
    }
    
    private func alertCallSecond(){
        //呼び出し処理
    }
    
}

まとめ

今回初めてiOSのアプリの開発をしており、実機はiphone5sを利用してます。
アラートが思ったより小さくて押しづらいのではないか?という不安がとりあえず。
わからないことだらけで文法とか調べながらですが、なかなか面白いですね。

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?