2016-09-13 41 views
3

我有一個子視圖,我已經重寫了preferredFocusedView。該子類有一個名爲viewToFocus的UIView。我檢查是否存在該視圖,如果它是我關注該視圖,如果不是,我會返回父級的preferredFocusedView。tvOS 10. PreferredFocusView已棄用。如何更改爲preferredFocusEnvironment?

因爲我更新到tvOS 10今天我收到以下錯誤:

'preferredFocusedView' is deprecated: first deprecated in tvOS 10.0 - Use -preferredFocusEnvironments instead.

我不能解釋preferredFocusEnvironment是如何實現的文檔中發現任何地方。在文檔中發現Supporting Focus within Your App,它說,

Override the preferredFocusedView to specify where focus should start by default.

我嘗試添加了UIFocusEnvironment協議,但我不知道怎麼用它取代「preferredFocusedView」的功能。

- (UIView *)preferredFocusedView { 
    if (self.viewToFocus) { 
     UIView *newView = self.viewToFocus; 
     self.viewToFocus = nil; 

     return newView; 
    } else { 
     return [super preferredFocusedView]; 
    } 
} 

感謝

回答

8

你需要傳遞的觀點數組作爲preferredFocusEnvironments呼叫,而不只是一個觀點,因爲它以前的結果。此視圖必須按焦點優先順序排列。所以,如果你有你的UIViewControllerpreferredFocusEnvironments財產3個UIButton項目可以實現以下幾點:

- (NSArray<id<UIFocusEnvironment>> *)preferredFocusEnvironments { 

    return @[self.b3, self.b2, self.b1]; 
} 

在你的情況,你只需要返回@[newView]結果。