사용자에게 메시지를 전달하는 방법

2022. 6. 9. 20:55iOS

1. 메시지창(알림)

알림창은 모달방식으로 화면 표시

액션 시트는 모달이 아니다.

샘플 코드를 알아보자.  UIAlertController 클래스를 이용하여 메시지 창을 구현하는 과정이다.

//메시지창 컨트롤러 인스턴트를 생성한다.

    let alert = UIAlertController(title: "알림", message: "UIAlertController 샘플 알림창입니다.", preferredStyle: UIAlertController.Style.alert)

 

    //메시지 창 컨트롤러에 들어갈 버튼 액션 객체를 생성한다.

    let cancel = UIAlertAction(title: "취소", style: UIAlertAction.Style.cancel)

 

    //메시지 창 컨트롤러에 버튼 액션을 추가한다

    alert.addAction(cancel)

 

    //메시지 창 컨트롤러를 표시한다.

    self.present(alert, animated:false)

 

   //UIAlertAction 클래스 초기화 구문에서 사용 되는 서번째 매개변수는 버튼을 클릭했을 때 실행될 구문. 

    UIAlertAction(title: "취소", style:.cancel, handler:{ (_) in })

 

메시지 창 실습 진행

추가적으로 버튼 생성 하기

        //취소 버튼

        let cancel = UIAlertAction(title: "취소", style: .cancel)

        //확인 버튼

        let ok = UIAlertAction(title: "확인", style: .default)

        alert.addAction(cancel)

        alert.addAction(ok)

이렇게 코드를 입력하여 버튼을 추가적으로 생성 할 수 있다.

 

 

//메시지창 객체 생성에 있어 알림창 혹은 액션 시트로 나타낼수 있는 명령어

        let alert = UIAlertController(title: "선택", message: "항목을 선택해주세요", preferredStyle: .alert)

==알림창

        let alert = UIAlertController(title: "선택", message: "항목을 선택해주세요", preferredStyle: .actionsheet)

== 액션시트로 나타난다

 

'iOS' 카테고리의 다른 글

Xcode 의 각 영역과 명칭  (0) 2022.07.02
델리게이트 패턴이란?  (0) 2022.06.13
다른 뷰 컨트롤러와 데이터 주고받기  (0) 2022.06.09
화면 전환의 종류  (0) 2022.06.06
인터페이스 빌더에 대해서(IB)  (0) 2022.06.06