개발언어/Swift

프로토콜 델리겟 패턴(protocol delegate pattern)

우주먼지쪼가리 2021. 8. 16. 16:45
반응형

main view controller와 popup view controlelr 연결(이벤트 수신, 발신 알림)

버튼을 하나 더 추가한다.

1.버튼을 하나 더 드래그해서 가져온다.

2.버튼 두개를 묶어서 embaded in을 하여서 stack view로 만들어준다.

3.spacing을 10준다.

//
//  Alert_Popup_ViewController.swift
//  Custom_PopUp_example


import UIKit

class Alert_Popup_ViewController: UIViewController {
    

    //구독하러가기 버튼
    @IBOutlet weak var subscribeBtn: UIButton!
    //bg button
    @IBOutlet weak var bgBtb: UIButton!
    
    //delegate 설정
    var myPopupDelegate : PopUpDelegate?
    
    //오픈 챗 버튼
    @IBOutlet weak var openkakaoBtn: UIButton!
    
    var subscribeBtnCompletionClosure: (() -> Void)? //아무행동도 안하지만, 알려주기만 한다.
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        print("Alert_Popup_ViewController 입니다")
        
        //모서리 둥글게
    
        subscribeBtn.layer.cornerRadius = 10
    }
    
    @IBAction func onBgBtnClicked(_ sender: Any) {
        print("창을 닫기 위한 배경 클릭 됨")
        //현재 창 닫기
        self.dismiss(animated: true, completion: nil)
    }
    //MARK: 오픈채팅방 가기
    @IBAction func openKakaoClicked(_ sender: Any) {
        print("오픈채팅방 바로가가기 클릭 됨")
        // tv버튼의 14번채널로 가는 버튼을 누른 거임 (발신기)
        self.dismiss(animated: true, completion: nil)
        myPopupDelegate?.onOpenChatBtnClicked()
        
    }
    
    @IBAction func subscribeClicked(_ sender: Any) {
        print("구독하러가기 버튼 클릭 됨")
        
        self.dismiss(animated: true, completion: nil)
        //컴플레션 블럭 호출
        if let subscribeBtnCompletionClosure = subscribeBtnCompletionClosure {
            //메인에 알림
            subscribeBtnCompletionClosure()
        }
    }
    
}
//
//  ViewController.swift
//  Custom_PopUp_example
//
//

import UIKit
//인터넷을 사용할거기 때문에
import WebKit

class ViewController: UIViewController,PopUpDelegate {
    //MARK: popup delegate method 연결시켜줌
    func onOpenChatBtnClicked() {
        //수신기
        print("뷰 컨트롤러의 onopenchatBtnClicked")
        let myChannelUrl = URL(string:"https://fffounding.tistory.com/")
        self.myWebview.load(URLRequest(url: myChannelUrl!))
    }
    

    //webView 끌어서 가져옴
    @IBOutlet weak var myWebview: WKWebView!
    
    //클릭 버튼 정의
    @IBOutlet weak var popUpBtn: UIButton!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    //버튼 클릭 시 실행
    @IBAction func createPopUpBtnClicked(_ sender: UIButton) {
        //팝업 버튼 클릭 시
        print("viewcontroller called")
        
        //스토리보드 가져오기
        let storyboard = UIStoryboard.init(name: "PopUp", bundle: nil)
        //스토리보드를 통해 popup view controller 가져오기
        let alertPopupVC = storyboard.instantiateViewController(withIdentifier: "AlertPopupVC") as! Alert_Popup_ViewController

        //팝업 효과 설정
        alertPopupVC.modalPresentationStyle = .overCurrentContext
        // 뷰컨트롤러가 사라짐
        alertPopupVC.modalTransitionStyle = .crossDissolve //스르르
        //메인에 알림
        alertPopupVC.subscribeBtnCompletionClosure = {
            print("컴플레션 블럭이 호출 됨")
            let myChannelUrl = URL(string:"https://fffounding.tistory.com/")
            self.myWebview.load(URLRequest(url: myChannelUrl!))
        }
        //수신기 발신기 연결시켜주는 부분
        alertPopupVC.myPopupDelegate = self
        
        //다른 뷰 컨트롤러를 보여주는거
        self.present(alertPopupVC, animated: true, completion: nil)
    }
    
}
//
//  PopUpDelegate.swift
//  Custom_PopUp_example
//


import Foundation


protocol PopUpDelegate {
    
    func onOpenChatBtnClicked()
}
반응형