본문 바로가기
iOS Swift/Study

[codebase UI 그리기] Button 누를시 다른 view 로 화면전환 하기

by 야고이 2024. 4. 22.
728x90

240422

 

로그인 버튼을 누르면 지정해 놓은 뷰로 이동해 보려한다

lazy var loginButton: UIButton = {
        var button = UIButton(type: .custom)
        button.setTitle("로그인", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .bold)
        button.backgroundColor = .clear
        button.layer.cornerRadius = 5
        button.clipsToBounds = true
        button.layer.borderWidth = 1   // 버튼의 테두리 두께 설정
        button.layer.borderColor = UIColor.white.cgColor  // 버튼의 테두리 색 설정
        button.isEnabled = true       // 버튼의 동작 설정 (처음에는 동작 off)
        
        
        //**********************************//
        button.addTarget(self, action: #selector(loginButtonClicked), for: .touchUpInside)
        return button
    }()
    
    @objc func loginButtonClicked() {
        print("clicked button")
        let mainPageViewController = MainPageViewController()
        navigationController?.pushViewController(mainPageViewController, animated: true)
    }

 

addTarget 메소드로 구현해주면 된다

@objc func 함수에서 원하는 뷰를 지정하여 주면 끝!

 

여담.

버튼에 isEnabled = false 로 해놓고 왜 안되지 !! 이랬음 ㅋㅋ

버튼 동작을 허용 해줘야해~

728x90

댓글