swift에서 Controller 또는 View를 가져오는 방법

1594 단어
현재view의view 가져오기
        ///  lineView
        ///
        /// - Parameter view:  view
        /// - Returns: wantView
        func findSubView (view: UIView){
            for subview in view.subviews {
                if subview.tag == 999 {// tag view 
                }
                findSubView(view: subview)
            }
        }
        
        DispatchQueue.mainAsync {
            findSubView(view: alert)
        }

윈도우의 최상위 컨트롤러 가져오기
    // 
   func currentVc() -> UIViewController {
        let keywindow = UIApplication.shared.keyWindow
        let firstView: UIView = (keywindow?.subviews.first)!
        let secondView: UIView = firstView.subviews.first!
        let vc = viewForController(view: secondView)
        return vc!
    }

    func viewForController(view:UIView)->UIViewController?{
        var next:UIView? = view
        repeat{
            if let nextResponder = next?.next, nextResponder is UIViewController {
                return (nextResponder as! UIViewController)
            }
            next = next?.superview
        }while next != nil
        return nil
    }
    

호출 방법:
    let vc = self.currentVc()// 
    if vc.childViewControllers.first is ManagerRootViewController {
         self.perform(#selector(ManagerRootViewController.disAction), on: Thread.main, with: nil, waitUntilDone: true)
    }// UINavgitionController, first rootViewController

Perform 사용:
     A B B xx ,perform 
    onThread: xx 
    waitUntilDone:true xx .

좋은 웹페이지 즐겨찾기