菜鸟教程小白 发表于 2022-12-11 19:34:48

ios - UIViewController 中的多个初始化步骤问题(不能使用多重继承)


                                            <p><p>我正在尝试使用封装在另一个基类中的一些初始化步骤来实现 UIPageViewController。</p>

<p>由于在 Swift 中不可能实现多重继承,我正在尝试使用协议(protocol),但我想触发一些封装在基类中的初始化步骤。</p>

<p> <a href="https://gist.github.com/michael-martinez/75a464a1719093a66e728094bd34457b" rel="noreferrer noopener nofollow">Here</a>是我写的基本 Controller 。</p>

<p>它封装了 Facebook Account Kit 插件以隐藏我的子 VC 不应该看到的连接信息(例如 import AccountKit 指令、AKFAccountKit 类实例)。</p>

<p>当我在标准类中使用它时,它可以工作:</p>

<pre><code>class ClientViewController: AccountKitBaseViewController { /*...*/ }
extension ClientViewController: AccountKitBaseViewControllerDelegate {/*...*/}
</code></pre>

<p>但如果我将 PageVC 用作客户端类,我将无法使用它:</p>

<pre><code>class ClientViewController: UIPageViewController, AccountKitBaseViewController { /* Error: Multiple inheritance from classes &#39;UIPageViewController&#39; and &#39;AccountKitBaseViewController&#39;*/ }
extension ClientViewController: AccountKitBaseViewControllerDelegate {/*...*/}
</code></pre>

<p>我怎样才能做到呢?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我想建议您将所有逻辑从 <code>AccountKitBaseViewController</code> 放到某个助手类中,并将此类的实例添加到您的 Controller 中。它将帮助您避免代码重复。您可以使用以下助手类:</p>

<pre><code>class SocialNetworkAssistant {

    //Put here all required propertie from AccountKitBaseViewController
    public var delegate: AccountKitBaseViewControllerDelegate?
    public var isUserLoggedIn: Bool = false
    private var _accountKit: AKFAccountKit!
    private var _pendingLoginViewController: AKFViewController?

    //Put here all required methods from AccountKitBaseViewController
    public func accountKitLogout(completion: (() -&gt; Swift.Void)? = nil) {

      guard isUserLoggedIn == true else { return }

      isUserLoggedIn = false
      _accountKit?.logOut()
      completion?()
    }

    // ... and so on
}
</code></pre>

<p>然后您可以将 <code>SocialNetworkAssistant</code> 的实例放入您的 Controller 中:</p>

<pre><code>class ClientViewControllerFirst : UIViewController, AKFViewControllerDelegate {

    private let socialNetworkAssistantInstance = SocialNetworkAssistant()

    /* AKFViewControllerDelegate methods implementation */
}

class ClientViewControllerSecond : UIPageViewController, AKFViewControllerDelegate {

    private let socialNetworkAssistantInstance = SocialNetworkAssistant()

    /* AKFViewControllerDelegate methods implementation */
}
</code></pre>

<p>您也可以使用桥接模式。将 <code>SocialNetworkAssistant</code> 作为所有将实现 <code>AKFViewControllerDelegate</code> 而不是您的 Controller 的类的基类。然后,您将在 Controller 中使用 <code>SocialNetworkAssistant</code> 的不同子类。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UIViewController 中的多个初始化步骤问题(不能使用多重继承),我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/48169570/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/48169570/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UIViewController 中的多个初始化步骤问题(不能使用多重继承)