반응형

//
//  ComposeViewController.swift
//  NhMemo
//


import UIKit

class ComposeViewController: UIViewController {

    //cancel 버튼 연결
    @IBAction func close(_ sender: Any) {
        //첫번째 파라미터 : true전달 시, 실행
        //두번째 파라미터 : 어떤 함수를 실행시키고 싶을 때, 클로저로 
        dismiss(animated: true, completion: nil)
    }
    
    //textView 저장을 위한 연결
    @IBOutlet weak var memoTextview: UITextView!
    
    //save 버튼 연결
    @IBAction func save(_ sender: Any) {
        
        //저장 버튼 클릭 시, memo안에 텍스트 저장 및 조건문 
        guard let memo = memoTextview.text,
              memo.count > 0 else {
                  //확장클래스 가져옴. 사용자가 메모를 입력하지 않을 시, 경고창 뜸
                  alert(message: "메모를 입력하세요.")
                  return
              }
        //guard 다음에 실행 됨.
        let newMemo = Memo(content: memo)
        //메모 저장
        Memo.dummyMemoList.append(newMemo)
        //창 닫음
        dismiss(animated: true, completion: nil)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
    
    }

    

}

 

메모를 저장할 시 사용 될 alertController, uiViewController를 상속하는 모든 클래스에서 사용할 수 있도록 확장(extension)!

.swift구현

//
//  UIViewControllerPlusAlert.swift
//  NhMemo
//
//

import UIKit

//메소드 확장(uiviewcontroller를 상속하는 모든 클래스에서 상속할 수 있도록)
extension UIViewController {
    func alert(title: String = "알림", message:String) {
        //알림창 구현!!
        
        
        //세번째 파라미터에서 .alert또는 .actionsheet로 쉽게 전환 가능
        let alert =  UIAlertController(title: title, message: message, preferredStyle: .alert)
        
        //알림창 버튼 구현, 세번째 파라미터 역할 : 버튼을 탭하였을 때, 실행 할 코드
        let okAction = UIAlertAction(title: "확인", style: .default, handler: nil)
        
        //알림창에 버튼 등록
        alert.addAction(okAction)
        
        //경고창을 화면에 표시
        present(alert, animated: true, completion: nil)
    }
}

스위프트에서 사용되는 UIAlertController의 세번째 파라미터 옵션 기본(왼) 옵션이 많음 + 아래쪽에서 생성(오)

 

반응형

+ Recent posts