2012-05-25 23 views
1

我試過這從幾種不同的方法,但這不按預期工作。這是一個混合的iOS應用程序,大約一半的屏幕UIViewControllers具有主UIWebView。處理UIWebView事件有很多類似的邏輯,特別是在錯誤處理方面。這些子類稍微專門用來處理正在呈現的屏幕。因此,類圖大致爲:使用MonoTouch與子類UIViewControllers不按預期方式綁定

UIViewController 
    +- BaseWebController <-- this class should bind to UIWebView 
     +-- ScreenAController 
     +-- ScreenBController 
     +-- ... 

其中BaseWebController.designer.cs確實是基本的。事實上,一旦連接好,它看起來像:

[Register ("BaseWebController")] 
partial class BaseWebController 
{ 
    [Outlet] 
    MonoTouch.UIKit.UIWebView WebView { get; set; } 

    void ReleaseDesignerOutlets() 
    { 
     if (WebView != null) { 
      WebView.Dispose(); 
      WebView = null; 
     } 
    } 
} 

的問題是:我得到了各種各樣的例外,因爲UIWebView中沒有正確綁定到的WebView實例。我得到各種消息,如'無法找到選擇器setWebView:在ScreenAController',但它歸結爲(我認爲)它試圖綁定父類(ScreenAController)並失敗。

問題:我錯過了這個綁定應該如何工作的東西嗎?本質上,我試圖保持這非常乾燥,並推動所有重複的代碼到基類。否則,我將最終爲每個屏幕重複相同的100行代碼和錯誤檢查。

TIA。

回答

0

@Stuart:

權。我結束了與你提到的非常相似的地方。我決定每個父類(ScreenAController,ScreenBController)結合相同的視圖,並實現由基座限定的抽象屬性:

protected abstract UIWebView ActiveWebView { get; } 

這基類設置在其構造的XIB文件。 ScreenAController,ScreenBController,...實現這個屬性,這個屬性實際上只是對該插座的引用。它儘可能幹,我可以得到。這幾乎是你說的。

/// <summary> 
    /// Provides access to the base class to handle most error handling. 
    /// </summary> 
    protected override UIWebView ActiveWebView { get { return this.WebView; } } 

@mhutch,我會仔細看看那個bug。我現在可以解決這個問題。

謝謝!

1

我想你是正確的關於繼承和約束不完全gel'ing。

可以通過添加可能危害:

[outlet] 
new UIWebView WebView 
{ 
    get { return base.WebView; } 
    set { base.WebView = value;} 
} 

到每個ScreenController類。

不完美DRY,但比複製所有類中的所有內容要好。

相關問題