How to get class name of parent ViewController in Swift?

后端 未结 7 1913
清歌不尽
清歌不尽 2021-02-07 03:27

Is there any way to get class name of parent VC in present (child) UIViewController? My \'child\' VC (push) has two \'parent\'UIViewControllers, so I w

7条回答
  •  时光说笑
    2021-02-07 03:47

    UPDATED TO SWIFT 5

    In your child view controller, you could try something like:

    guard let parent = self.presentingViewController else{
        // ...some code
        return
    }
    //presented by parent view controller 1
    if parent.isKind(of: Parent1.self){
        // do something
    }else{
        //presented by parent view controller 2
    }
    

    I recommend you to place this logic in your viewWillAppear method because when viewDidLoad is called, there is no guarantee that the view controller hierarchy is loaded in the navigation tree and like a consequence of this, the presentingViewController property of your child view controller might be nil

提交回复
热议问题