티스토리 뷰

반응형
addTarget 정리
 

Apple Developer Documentation

 

developer.apple.com

addTarget(_:action:for:)

  • addTarget은 UIControl 클래스 안에 있는 인스턴스 메서드입니다.
  • UIButton과 같이 UIControl 클래스 자식들은 사용이 가능합니다.
  • 객체에 접근하여 특정 이벤트가 발생할 때 마다 자신이 작성한 메서드를 호출시킬 수 있습니다.

선언

func addTarget(
    _ target: Any?,
    action: Selector,
    for controlEvents: UIControl.Event
)

매개변수

target 

  •  대상 개체, 즉 action메서드가 호출된 개체입니다. 를 지정 nil하면 UIKit은 지정된 작업 메시지에 응답하는 개체에 대한 응답자 체인을 검색하고 해당 개체에 메시지를 전달합니다.

action

  • 호출할 작업 메서드를 식별하는 선택기입니다. 서명이 Listing 1 의 서명과 일치하는 선택기를 지정할 수 있습니다 . 이 매개변수는 가 아니어야 합니다 nil.

controlEvents

  • 작업 메서드가 호출되는 컨트롤별 이벤트를 지정하는 비트 마스크입니다. 항상 하나 이상의 상수를 지정하십시오. 가능한 상수 목록은 를 참조하십시오 .

실습

        let cgvButton: UIButton = {
        let button = UIButton()
        button.setTitle(" title ", for: .normal)
        button.addTarget(self, action: #selector(moveToTestVC), for: .touchUpInside)
        button.backgroundColor = .secondarySystemBackground
        button.setTitleColor(.systemIndigo, for: .normal)
        button.layer.cornerRadius = 8.0
        
        return button
    }()
    
        @objc func presentToCgvViewController() {
        let testVC = UINavigationController(rootViewController: TestViewController())
        cgvVC.modalPresentationStyle = .automatic
        present(testVC, animated: true)
    }
  • Button을 누르면 moveToTestVC 메서드가 호출되어 TestViewController로 화면이 전환됩니다.
반응형
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함