简体   繁体   中英

swift detection from another viewController - iOS swift

I have a ViewController that contains a segmented control. this viewController contains a view (Container view) that represent two other viewControllers (viewController A and viewController B) depending on the segment control item. i want to be able to detect swipes from viewController A or B and according to the swipe i move from one to another.

how is it possible to reach this goal? i thought i might be able to do it using delegates but i did not know or understand how to.

Could anyone help me by giving me an idea of how to do it?

Yes, you could do it by making the parent view controller the delegate of the 2 child view controllers, and setting up a protocol that lets you notify the parent about the transition between view controllers.

However, why not keep it simple and attach a swipe (or pan) gesture recognizer to the container view in the parent view instead? That way the calls from the gesture recognizer get delivered directly to the parent view controller rather than having to go through a delegate link. Unless you need pan or swipe gesture support in the child view controller(s) that approach should work and be simpler to implement.

Edit:

I have an app on Github called Test (horrible name) that illustrates using delegates to communicate between a parent and child view controller. (The app is written in Objective-C.) In my demo app the child view controller does not get replaced, but still illustrates setting up a delegate to communicate from the child to the parent. In fact in that app there is communication in both directions, from child to parent and from parent to child.

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