简体   繁体   中英

Communication from child view controller to container view controller

I am building a view controller hierarchy. I have a child view controller 2 levels deep from the container view controller.

This child view controller has a button to perform a certain action.

What's the best way to have this communicated to the container view controller. I have tried sending a message using the responder chain. Having a delegate seems super tricky given that it's 2 levels deep. Is there a better way?

You can use this function to find the contain ViewController,replace the ContainViewController with the Class you have

- (ContainViewController *)containViewController
{
    UIViewController *iter = self.parentViewController;
    while (iter) {
        if ([iter isKindOfClass:[ContainViewController class]]) {
            return (ContainViewController *)iter;
        } else if (iter.parentViewController && iter.parentViewController != iter) {
            iter = iter.parentViewController;
        } else {
            iter = nil;
        }
    }  
    return nil;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM