Last active
January 19, 2017 05:36
-
-
Save mosluce/a58dcb4e80c1054998c96db9bc74a074 to your computer and use it in GitHub Desktop.
Multi ViewControllers to One
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class AVC: UIViewController { | |
| weak var delegate: AVCDelegate? | |
| // TODO: implement | |
| func hello() { | |
| // 請 Main 幫忙做點事 | |
| self.delegate?.somethingFromA(self) | |
| } | |
| // 可以被 Main 或其他 VC 呼叫 | |
| public func foo() { | |
| } | |
| } | |
| protocol AVCDelegate: class { | |
| func somethingFromA(_ a: AVC) | |
| } | |
| class BVC: UIViewController { | |
| weak var delegate: BVCDelegate? | |
| // TODO: implement | |
| func hello() { | |
| // 請 Main 幫忙做點事 | |
| self.delegate?.somethingFromB(self) | |
| } | |
| //同上 | |
| public func bar() { | |
| } | |
| } | |
| protocol BVCDelegate: class { | |
| func somethingFromB(_ b: BVC) | |
| } | |
| class MainVC: UIViewController { | |
| var avc: AVC! | |
| var bvc: BVC! | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| // 初始化兩個子 VC | |
| self.avc = AVC() | |
| self.bvc = BVC() | |
| self.avc.delegate = self | |
| self.bvc.delegate = self | |
| self.addChildViewController(avc) | |
| self.addChildViewController(bvc) | |
| self.view.addSubview(avc.view) | |
| self.view.addSubview(bvc.view) | |
| // TODO: 設定Layout | |
| } | |
| } | |
| extension MainVC: AVCDelegate, BVCDelegate { | |
| func somethingFromA(_ a: AVC) { | |
| // 來自 AVC 的指派 | |
| // TODO: 相應的處理 | |
| self.bvc.foo() | |
| // TODO: 或其他程式 | |
| } | |
| func somethingFromB(_ b: BVC) { | |
| // 來自 BVC 的指派 | |
| // TODO: 做些什麼 | |
| self.avc.bar() | |
| // TODO: 也不一定是和 AVC 或 BVC 互動 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment