2015-04-05 72 views
3

我實現自定義PopoverBackgroundView實現,如在Swift documentation規定我必須實現方法如下:UIPopoverBackgroundView contentViewInsets必須由子類

class SettingsPopoverBackgroundView: UIPopoverBackgroundView { 

override var arrowOffset: CGFloat { 
    get { 
     return 0.0 
    } 
    set { 
     super.arrowOffset = newValue 
    } 

} 

override var arrowDirection: UIPopoverArrowDirection { 
    get { 
     return UIPopoverArrowDirection.Up 
    } 
    set { 
     super.arrowDirection = newValue 
    } 
} 

func contentViewInsets() -> UIEdgeInsets { 
    return UIEdgeInsetsMake(10, 10, 10, 10) 
} 

func arrowBase() -> CGFloat { 
    return 2.0 
} 

func arrowHeight() -> CGFloat { 
    return 2.0 
} 
} 

不過,我仍然得到錯誤:

UIPopoverBackgroundView contentViewInsets must be implemented by subclassers.

看起來像蘋果有一些亂碼異常,因爲這個子類as can be seen here,因爲我實現了contentViewInsets,但仍然得到錯誤。

這是怎麼了設置背景類的酥料餅在prepareForSegue方法:

popover.popoverBackgroundViewClass = SettingsPopoverBackgroundView.self 

是不是?

任何人都可以看到我做錯了什麼?

+0

我複製了你的代碼,我沒有得到任何錯誤。在構建過程中還是在運行時,您是否收到此錯誤? – rdelmar 2015-04-05 17:02:24

+0

只是爲了確保您確實覆蓋了正確的方法:將'overwrite'關鍵字添加到最後3個函數中。 – luk2302 2015-04-05 17:02:27

+0

@rdelmar運行時。 – andriosr 2015-04-05 19:13:16

回答

6

contentViewInsets(),arrowBase()arrowHeight()都是類函數,因此您需要在func之前爲這些函數覆蓋類。

對於arrowOffsetarrowDirection,文檔說您的方法不能調用超級。我不確定你需要在這些設置器中放置什麼(取決於你想要做什麼),但是如果你將它們留空,應用程序應該運行沒有錯誤(儘管你不會看到箭頭)。

+0

非常感謝!我得到了另一個錯誤,但肯定解決了第一個錯誤。新的錯誤是:setValue:forUndefinedKey:]:這個類不是關鍵值設置視圖的密鑰值編碼。如果你可以幫助以及:) – andriosr 2015-04-05 20:26:17

+1

@andriosr該錯誤通常是由於引用了某些東西,在這種情況下「settingsView」引起的,這在您的代碼中並不存在。你應該在你使用settingsView的某個地方檢查你的故事板(或者如果你有他們的xibs)。 – rdelmar 2015-04-05 20:29:12

+0

令人驚歎的,非常感謝你!問題解決了。 – andriosr 2015-04-05 20:31:47

相關問題