enum과 if case문을 이용해서 ViewContoller 이동하기 예제

 

enum 값 설정


enum SubView: Equatable {
    case Login
    case Register
        
    var storyboard: String {
        switch self {
        case .Login:            return "Main"
        case .Register:         return "Common"
        }
    }
    
    var className: String {
        switch self {
        case .Login:            return "LoginViewController"
        case .Register:         return "RegisterViewController"
        }
    }
}

 

ViewController 이동에 필요한 메인 enum

enum VCPATH {
    case SubViewPath(SubView)
}

 

메인탭 클릭시 VCPATH 설정 

@IBAction func loginButton(_ sender: UIButton) {
	let login = VCPATH.SubViewPath(.Login)
	GlobalInterface.commonVC(action: login, currentVC: self)
}

@IBAction func registerButton(_ sender: UIButton) {
	let register = VCPATH.SubViewPath(.Register)
	GlobalInterface.commonVC(action: register, currentVC: self)
}

 

ViewController present 

class GlobalInterface {
	// MARK: 네이게이션 뷰 / 탭바 뷰 컨트롤 이동
    static func commonVC(action: VCPATH, currentVC: UIViewController) {
        guard let topVC = UIApplication.currentTopViewController() else {
           return
        }
        
        if case VCPATH.SubViewPath(let subview) = action {
            switch subview {
            case .Login:
            	let vc = UIStoryboard(name: subview.storyboard, bundle: nil).instantiateViewController(withIdentifier: subview.className)

                vc.loginID = "abcd"
			
                vc.modalPresentationStyle = .overFullScreen
                topVC.present(vc, animated: true, completion: nil)
                .... 생략 ....
            case .Register:
	            let vc = UIStoryboard(name: subview.storyboard, bundle: nil).instantiateViewController(withIdentifier: subview.className)
				
                vc.register = "abcd"
			
                vc.modalPresentationStyle = .overFullScreen
                topVC.present(vc, animated: true, completion: nil)

            }
        }   
    }
}

'Swift > 문법' 카테고리의 다른 글

[SWIFT]Dictionary merging / sorted  (0) 2023.07.25
[SWIFT]Array zip  (0) 2023.07.25
[SWIFT]UnsafeMutablePointer<AudioBufferList> to AVAudioPCMBuffer  (0) 2023.07.25
[SWIFT]Tuples  (0) 2023.07.25
[SWIFT]Array  (0) 2023.07.25

+ Recent posts