UIAlertController | UIAlertAction

 

더 좋은 방법이 있을까요?

 

 

Alert.swift

class Alert: NSObject {
    class func showAlert(title :String, message :String, actions :[String], completion :@escaping (_ text :String, _ index :NSInteger) -> Void) -> Void {
        let alert :UIAlertController    = UIAlertController.init(title: title, message: message, preferredStyle: .alert)
        for i in 0 ..< actions.count {
            let action :UIAlertAction = UIAlertAction.init(title: actions[i],
                                                           style: .default) { (action) in
                completion(actions[i], i)
            }
            alert.addAction(action)
        }
        alert.modalPresentationStyle = .popover
        
        // getTopViewController : 최상단 뷰 컨트롤러
        getTopViewController()?.present(alert, animated: true, completion: {
            
        })
    }
    
    class func showAction(title :String, message :String, actions :[String], completion :@escaping (_ text :String, _ index :NSInteger) -> Void) -> Void {
        let alert :UIAlertController    = UIAlertController.init(title: title, message: message, preferredStyle: .actionSheet)
        for i in 0 ..< actions.count {
            let action :UIAlertAction = UIAlertAction.init(title: actions[i],
                                                           style: i == (actions.count-1) ? .cancel : .default) { (action) in
                completion(actions[i], i)
            }
            alert.addAction(action)
        }
        alert.modalPresentationStyle = .popover
        alert.view.tintColor = UIColor.black

	// getTopViewController : 최상단 뷰 컨트롤러
        getTopViewController()?.present(alert, animated: true, completion: {
            
        })
    }
}

 

사용방법

 

Alert 사용방법

Alert.showAlert(title: "[ 타이틀 ]",
                        message: "[ 메세지 ]]",
                        actions: ["액션 - 1","액션 - 2","액션 - 3","액션 - 4","액션 - 5","닫기"]) { (text, index) in
    if text != "닫기" {
                
    }
}
더보기

Alert 결과화면

showAlert 호출 화면

 

 

ActionSheet 사용방법

Alert.showAction(title: "[ 타이틀 ]",
                        message: "[ 메세지 ]]",
                        actions: ["액션 - 1","액션 - 2","액션 - 3","액션 - 4","액션 - 5","닫기"]) { (text, index) in
    if text != "닫기" {
                
    }
}
더보기

ActionSheet 결과화면

 

showAction 호출 화면

'[ 자기개발 ] > [ Swift ]' 카테고리의 다른 글

Toast.Swift  (0) 2021.04.27
UIColor+Extension.swift  (0) 2020.11.19

+ Recent posts