Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

개발자 되버리기

Swift4 버튼 클릭시 경고창 띄우기 본문

개발/Swift

Swift4 버튼 클릭시 경고창 띄우기

구본익 2019. 1. 26. 18:38


웹에서 Alert 로 창을 하나 띄우듯이 앱에서도 창을 띄울 수 있습니다.


소스코드입니다.


class ViewController: UIViewController {

    

    override func viewDidLoad() {

        super.viewDidLoad()

    }

    

    @IBAction func alert_Button(_ sender: UIButton) {

        Output_Alert(title: "경고!", message: "경고창입니다.", text: "확인")


    }

    

    func Output_Alert(title : String, message : String, text : String) {

        let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)

        let okButton = UIAlertAction(title: text, style: UIAlertAction.Style.cancel, handler: nil)

        alertController.addAction(okButton)

        return self.present(alertController, animated: true, completion: nil)

    }



다른곳에서 예제를 찾아보면 다른식으로 쓰여있는데 저는 경고창을 띄울 일이 많을 것 같아서 함수로 만들어 놨습니다.



앱을 실행하면 아래와 같은 화면이 뜹니다. "Hello World!" 라는 문구는 신경쓰지 않으셔도 됩니다.


파란색으로 된 button을 클릭하면 경고창이 뜹니다.


Comments