2015-09-24 14 views

Trả lời

18

Cuối cùng đã tự tìm ra. Bạn phải ghi đè thuộc tính preferredFoucsedView của UIView hoặc UIViewController.

Trong nhanh chóng nó đi như thế này:

func myClickHandler() { 
    someCondition = true 
    self.setNeedsFocusUpdate() 
    self.updateFocusIfNeeded() 
    someCondition = false 
} 

override weak var preferredFocusedView: UIView? { 
    if someCondition { 
     return theViewYouWant 
    } else { 
     return defaultView 
    } 
} 

Tôi không thể nhớ làm thế nào để ghi đè lên thu khí trong Objective-C vì vậy nếu ai đó muốn gửi rằng tôi sẽ chỉnh sửa câu trả lời.

+0

Cảm ơn bạn. Tôi đã xem xét việc làm như thế này, nhưng có vẻ như soooooo lộn xộn để đặt tất cả những "someConditions" trên khắp nơi. Sẽ dễ dàng hơn rất nhiều nếu bạn chỉ có thể setPreferredFocusedView ... –

+0

Nhưng, chờ đã. Làm thế nào để làm việc này khi tôi muốn thay đổi tập trung vào một viewcontroller? –

+2

Tôi đồng ý. Hy vọng rằng họ sẽ thêm nó. Ngoài ra để tránh tất cả 'someCondition', tôi đã thêm thuộc tính' viewToFocus' 'UIView' vào viewController của mình. Sau đó, trong getter, tôi kiểm tra nếu xem đó không phải là nil và trả lại nó. Nếu không, hãy quay lại chế độ xem mặc định. – Slayter

5

đây là mục tiêu C

- (UIView *)preferredFocusedView 
{ 
    if (someCondition) { 
     // this is if your menu is a tableview 
     NSIndexPath *ip = [NSIndexPath indexPathForRow:2 inSection:0]; 
     UITableViewCell * cell = [self.categoryTableView cellForRowAtIndexPath:ip]; 

     return cell; 
    } 

    return self.view.preferredFocusedView; 

} 

trong viewDidLoad hoặc xem bạn đã xuất hiện làm một cái gì đó như thế này:

UIFocusGuide *focusGuide = [[UIFocusGuide alloc]init]; 
    focusGuide.preferredFocusedView = [self preferredFocusedView]; 
    [self.view addLayoutGuide:focusGuide]; 

nếu bạn muốn làm điều đó khi nó lần đầu tiên ra mắt

+0

Tôi nhận được nill cho các tế bào và tôi đang sử dụng collectionview thay vì tableview! Bất kỳ ý tưởng tại sao nó là nil? –

14

đây là một triển khai khác dựa trên Slayters trả lời ở trên. Nó hơi tao nhã hơn tôi nghĩ hơn là sử dụng các boolean có điều kiện.

Đặt này trong viewController bạn

var viewToFocus: UIView? = nil { 
    didSet { 
     if viewToFocus != nil { 
      self.setNeedsFocusUpdate(); 
      self.updateFocusIfNeeded(); 
     } 
    } 
} 

override weak var preferredFocusedView: UIView? { 
    if viewToFocus != nil { 
     return viewToFocus; 
    } else { 
     return super.preferredFocusedView; 
    } 
} 

Sau đó, để sử dụng nó trong mã của bạn

viewToFocus = myUIView; 
+0

điều này đã không được chấp nhận trong iOS 10. đề xuất sử dụng preferredFocusEnvironments – nyxee

0

@ câu trả lời elu5ion 's, nhưng trong Objective-C đầu tiên tuyên bố:

@property (nonatomic) UIView *preferredView; 

Đặt các phương thức sau:

-(void)setPreferredView:(UIView *)preferredView{ 
     if (preferredView != nil) { 
     _preferredView = nil; 
     UIFocusGuide *focusGuide = [[UIFocusGuide alloc]init]; 
     [self.view addLayoutGuide:focusGuide]; 
     focusGuide.preferredFocusedView = [self preferredFocusedView]; 
     [self setNeedsFocusUpdate]; 
     [self updateFocusIfNeeded]; 
    } 
    _preferredView = preferredView; 
} 


- (UIView *)preferredFocusedView { 
    if (_preferredView) { 
      return _preferredView; 
    } 
    return self.view.preferredFocusedView; 
} 
0

Đây là một tốt đẹp chút Swift đoạn 2 copy/paste:

var myPreferredFocusedView: UIView? 
override var preferredFocusedView: UIView? { 
    return myPreferredFocusedView 
} 
func updateFocus(to view: UIView) { 
    myPreferredFocusedView = napDoneView 
    setNeedsFocusUpdate() 
    updateFocusIfNeeded() 
} 

Sử dụng nó như thế này:

updateFocus(to: someAwesomeView) 
9

Đối với ios 10 bạn nên sử dụng preferredFocusEnvironments thay vì preferredFocusedView.

Trong ví dụ dưới đây nếu bạn muốn tập trung vào btnEnglish thì hãy xem mã bên dưới.

@IBOutlet weak var btnEnglish:UIButton! 

override var preferredFocusEnvironments : [UIFocusEnvironment] { 

    //condition 
    return [btnEnglish] 
} 
override func viewDidLoad() { 
    super.viewDidLoad() 

    self.setNeedsFocusUpdate() 
    self.updateFocusIfNeeded() 
} 
+0

Đây phải là câu trả lời được chấp nhận ngay bây giờ vì 'preferredFocusedView' đang bị khấu hao. –

Các vấn đề liên quan