반응형

함수 정의,  UIViewController대신에 원하는 타입의 부모클래스를 넣으면 됨

func isAleadyVC<T>(_ vcs:[UIViewController]?, _ tp: T.Type) -> UIViewController? {
	for i in 0 ..< vcs!.count {
		if (vcs![i] as? UINavigationController) != nil {
			let nc = vcs![i] as! UINavigationController
			if let vc = isAleadyVC(nc.children, tp) {
				return vc
			}
		}
		else if (vcs![i] as? UITabBarController) != nil {
			let tc = vcs![i] as! UITabBarController
			if let vc = isAleadyVC(tc.children, tp) {
				return vc
			}
		}
		else { // always UIViewController
			let vc = vcs![i]
			if type(of: vc) == tp {
				return vc
			}
			if let vc = isAleadyVC(vc.children, tp) {
				return vc
			}
		}
	}
	return nil
}

 

사용

if let homeVC = isAleadyVC(self.window?.rootViewController?.children, HomeVC.self) as? HomeVC {
	...
}

'develop > iOS' 카테고리의 다른 글

ios 탈옥폰 체크(swift)  (0) 2020.04.23
ios 탈옥폰 체크  (0) 2019.12.12
xcode에서 개발/운영 환경 설정  (0) 2019.03.28
swift에서 macro define 사용하기  (0) 2019.03.28
swift용 SQLite 클래스  (0) 2018.07.13

+ Recent posts