Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem tabbing to field when adding and removing view

I have an scope bar containing a NSSearchField. The bar can be shown and hidden using a menu item. I generate this bar by creating a new NSViewController (and loading a new view from the XIB). When the bar is shown, I do a addSubview: to the window's contentView; when the bar is hidden, I do removeFromSuperview to the view within the view controller.

If when I launch the app and the bar is already opened, hitting tab toggles between the main view within the window (a table view) and the search field in the scope bar. If I launch the app and the bar isn't already shown, once I do show the bar I can tab from the table view to the search field, but not the other way.

Once I remove the scope bar for the first time, then show it again, I can no longer tab between the search field and the table view, no matter which view is currently selected.

Is there something I need to be doing besides addSubview: and removeFromSuperview? I can't wrap my head around why this won't work, and especially why I get different behaviors if the bar is shown on launch or not.

like image 739
livings124 Avatar asked Dec 27 '25 21:12

livings124


1 Answers

You need to set the nextKeyView of both views if you want to control what happens when you hit the tab key.

[yourTableView setNextKeyView:yourSearchField];
[yourSearchField setNextKeyView:yourTableView];

However, you need to be careful because you can break the automatically constructed key-view loop. This article has more detail on how to handle this situation.

like image 113
Rob Keniger Avatar answered Dec 30 '25 17:12

Rob Keniger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!