Cocoa - Get element that had focus before an action - swift

I have a Window with multiple NSTableView elements where I can select rows.
Now when I press a button an action is triggered and I need to do stuff depending on which element in which NSTableView was selected before the click.
How do I do this?
I can check using the selectedRowIndexes but this is set for all tableViews that has selected items and not only for the one that the user wants the action for. (the last one that was selected)

Related

NSCollectionView - Not able to tab through fields after first row

My NSCollectionView uses a custom NSCollectionViewItem which contains 2 NSComboBox, 1 NSTextField and 1 NSDatePicker. When running my app and displaying multiple NSCollectionViewItems, I am able to tab through the comboboxes and the textfield of the first item, but none of the items that follow. If I select the textfield in the second or third item and hit the tab key, nothing happens.
I would like to tab through each component in my NSCollectionViewItem and then continue through to the next component in the next NSCollectionViewItem. I expected this to be the default behavior.
Because it works for the first item in my NSCollectionView, I know that it can work for the rest.
Is there something I need to set while NSCollectionView is displaying the items?
Switch on Recalculates View Loop of the window. Side effect: nextKeyView of the views isn't used.

How to use first responder to keep only one cell selected at the maximum in a view with multiple table views?

I have a scrollview which has several table views as sub views...
say that I have selected a cell in one of the sub views. Now when I select a cell from another view, the cell I selected previously should not be highlighted.
How do I do this? I know I can do this using the first responder, but I am not sure how to do it.
Would anyone be able to help me out in this?
Thanks
From the apple interface guidlines:
Always provide feedback when users select a list item. Users expect a table row to highlight briefly when they tap a selectable item in it. After tapping, users expect an immediate action to occur: Either a new view appears or the row displays a checkmark to indicate that the item has been selected or enabled.
In rare cases, a row might remain highlighted when secondary details or controls related to the row item are displayed in the same screen. However, this is not encouraged because it is difficult to display simultaneously a list of choices, a selected item, and related details or controls without creating an uncomfortably crowded layout.
I don't think apple will approve an app that leaves a row highlighted (I had an app reject for that very reason). You should perform an action and then immediately unhighlight it. In some rare cases you can leave it highlighted while showing a related view.
However, if you store the current cell you have selected, you can call -deselectRowAtIndexPath:animated: on the tableview to deselect a row.

Adding a button to last UITableView row

I'm developing an app where the user can choose between a number of included songs. I also want the user to be able to choose a song from his/her iPod Library.
Currently the song is choosen by selecting it in a UITableView. So I figure I would like to add a new row at the end of the table and make it a button that will fire a MPMediaPickerController. All songs are placed in an array consisting of their names.
My question is how I add this last row? And also how I can "save" the selected song (or the path to it) to be used in the parent viewcontroller?
Well, you can use the UITableViewCell directly as a button itself, so when a user clicks on the last row, the action is being executed. But if I understand you right, you want to add a specific extra button as a subview of a UITableViewCell. That means if the cell (the last row) is being built, you compose your button, add its target and action, and add the button just as simple subview of the cell.
Well, the parent should now receive the message that the button has been pushed. I would do this using NSNotification, that is very easy to use, just check out the Apple documentation and take a look at an example. You can even send the selected song or its name path via the notification directly to the parent controller, where you can handle this notification.

Row selection when dragging over a UITableView with scrolling disabled

If one were to create a standard grouped table view consisting of two rows and then touch down on the first row, the cell would highlight. If one were to then drag one's finger down, the selection of the row cancels, and the table view begins to move with the drag.
Imagine the same situation, but with table view scrolling disabled via tableView.scrollEnabled = NO. Now, when one has selected a row and begins to drag, the row deselects and the table remains static.
I have two questions:
How can I ensure the row isn't deselected when one selects and drags within the confines of the row?
When one drags from the first row to the second row, how can I ensure that the first row is deselected and the second row becomes selected?
For an example of this functionality in action, open the Clock app and select the Alarm tab bar item. Tap the top right plus button and a modal view will appear presenting four rows. Tap down on the first row, drag onto the second, and you'll see the selection move across rows while the table itself remains static. How is this achieved?
On a tableView with scrollEnabled = NO when you touch on any cell you get a didHighlightRowAtIndexPath, and when you start to drag you get didUnhighlightRowAtIndexPath (even if you stay within the cell). The cell is not selected or deselected when you drag, and no further cells highlight/unhighlight on that touch.
There is a shouldHighlighRowAtIndexPath method that is called, where you can decide if the touch should highlight the row or not, but not a shouldUnhighlight method.
If the 'flash' of the highlight/unhighlight bothers you, you could use the shouldHighlight method to just return NO. Or you could return NO if a cell is selected.

UIPickerView - Selects row too fast

I am currently using a UIPIckerView in my app to allow a user to select from a list of options. The problem is that there isn't enough of a delay when the user stops spinning the wheel and it is selecting a value before the user has a chance to scroll further down the list.
Is there a way to override the default behavior that selects the row as soon as the wheel stops spinning and the user removes their finger? I see Mobile Safari includes a "Done" button which would be great.
I can provide code if necessary (not sure how it would help).
Thanks!
You can add this manually; just add a done button to the view that holds the UIPicker, and have IT do whatever action you're currently performing in – pickerView:didSelectRow:inComponent:.
The UIPickerView automatically selects which ever row stops in the center. It does not work like a table but more like a popup menu. As such, you can't use a picker view like a button to call an action because it will trigger the moment the user stops moving it whether that represents their final choice or not.
Instead, as noted previously, you need a second control element (usually a button) to call the action that makes use of the pickerview's selection.