How to get class name of parent ViewController in Swift?

后端 未结 7 1884
清歌不尽
清歌不尽 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

    0 讨论(0)
  • 2021-02-07 03:47

    Simply use view.parentViewController and eventually its title property.

    0 讨论(0)
  • 2021-02-07 03:47

    Updated to Swift 4

    Swift 4:

    if let parentVC = self.parent {
        if parentVC is someViewControllerr {
            // parentVC is someViewController
        } else if parentVC is anotherViewController {
            // parentVC is anotherViewController
        }
    }
    

    Swift 3:

    if let parentVC = self.parentViewController {
        if let parentVC = parentVC as? someViewController {
            // parentVC is someViewController
        } else if let parentVC = parentVC as? anotherViewController {
            // parentVC is anotherViewController
        }
    }
    
    0 讨论(0)
  • 2021-02-07 03:53

    First add extension in view controller

    extension UIView {
        var parentViewController: UIViewController? {
            var parentResponder: UIResponder? = self
            while parentResponder != nil {
                parentResponder = parentResponder!.next
                if let viewController = parentResponder as? UIViewController {
                    return viewController
                }
            }
            return nil
        }
    }
    

    Now Use it Like this

    let vc = self.parentViewController
    

    or

    if let parentVC = self.parentViewController {
        if let parentVC = parentVC as? someViewController {
            // parentVC is someViewController
        } else if let parentVC = parentVC as? anotherViewController {
            // parentVC is anotherViewController
        }
    }
    
    0 讨论(0)
  • 2021-02-07 03:56

    try to pick the child from the children array as following

    if let parentVC = self.parent {
    
                if let parentVC = parentVC.children[0] as? someViewController {}}
    
    0 讨论(0)
  • 2021-02-07 04:01

    Here's one approach:

    if let parentVC = self.parentViewController {
        if let parentVC = parentVC as? someViewController {
            // parentVC is someViewController
        } else if let parentVC = parentVC as? anotherViewController {
            // parentVC is anotherViewController
        }
    }
    

    First, assign and optionally unwrap self.parentViewController.

    Second, use optional casting as?, and assign the view controller to parentVC if it works.

    That said, this is a code smell - child view controllers should typically have no idea who their parent view controllers are. Whatever problem you're solving, you should probably solve it with tell, don't ask and delegation instead.

    0 讨论(0)
提交回复
热议问题