Toy Project/iOS Application 쌩기초

2. ViewController을 통해 화면출력

개발자킹콩 2021. 1. 28. 02:00

@IBAction func hello(_ sender: Any) {

        let alert = UIAlertController(title: "hello", message: "My First App!!", preferredStyle: .alert)

        let action = UIAlertAction(title: "OK", style: .default, handler: nil)

        alert.addAction(action)

        

        present(alert, animated: true, completion: nil)

    }

 

UIAlertController로 팝업을 만들게 되고 제목과 메세지를 입력한다.

그후 밑의 버튼은 UIAlertAction으로 만들게 되고 버튼이름은 설정가능하다. 버튼을 누르면 어떤 기능을 할지는 handler로 설정가능 하지만 아무것도 안하도록 설정하였다.

alert에 해당 Action을 파라미터로 보내 두개를 연결해주었고,

present를 통해 alert를 띄운다. 

(코드에 대한 이해도 중요하지만 이것은 팝업을 만드는 세트로 생각하면 좋음)

 

코드에서 라인에 동그라미에 갖다 대면 해당 코드가 어디와 연결되어 있는지 확인할 수 있다.

그리고 오른쪽 상단에 오른쪽인 Connetion inspector 클릭하면 해당 버튼같은것이 ViewController hello 연결되어 있음을 확인할 있다.