UIKIT

알림창

밤새는 탐험가89 2024. 2. 1. 14:01

 

 

https://developer.apple.com/documentation/uikit/windows_and_screens/getting_the_user_s_attention_with_alerts_and_action_sheets

 

Getting the user’s attention with alerts and action sheets | Apple Developer Documentation

Present important information to the user or prompt the user about an important choice.

developer.apple.com

 

 

 

 

 

 

기본 알림창 버튼 생성 

 

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 기본 알림창 버튼 생성
        let defaultalertBtn = UIButton(type: .system)
        defaultalertBtn.frame = CGRect(x: 0, y: 100, width: 100, height: 30)
        defaultalertBtn.center.x = self.view.frame.width / 2
        defaultalertBtn.setTitle("기본 알림창", for: .normal)
        defaultalertBtn.addTarget(self, action: #selector(defaultAlert(_:)), for: .touchUpInside)
        
        self.view.addSubview(defaultalertBtn)
        
    }
}

 

 

defaultAlert(_: ) 메소드가 호출되면 기본 알림창이 실행 

 

    @objc func defaultAlert(_ sender: Any) {
        
        // 알림창 정의
        let alert = UIAlertController(title: "알림창", message: "기본 메시지가 들어가는 곳", preferredStyle: .alert)
        
        // 버튼 정의
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
        let okAction = UIAlertAction(title: "OK", style: .default)
        
        // 버튼을 알림창에 추가
        alert.addAction(cancelAction)
        alert.addAction(okAction)
        
        // 알림창을 화면에 표시한다.
        self.present(alert, animated: false)
    }