WatchKit context menu, how to tell which row it was for? - xcode6

I have a WKInterfaceTable in my interface controller and have added a Context Menu to the controller. On tap holding a specific row, the row depresses and displays the context menu. How do I know what row the menu is for?

The context menu is for the entire view, it has no concept of where you tapped.

The following gets called on your WKInterfaceController.
- (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex

Related

NSMenu click in OS X

How can I detect when the menu in menu bar is clicked? I mean when whatever is clicked like for example "file" menu not a concrete position in the menu.
I tried to connect IBActions from menu items in interface builder from my code, but it does not work from the main menu item like "File" only from the submenus within this menu
First of all, what are you trying to achieve in the big picture? Perhaps knowing when the menu bar has been clicked is not the best approach.
You might be able to achieve what you're interested in by observing the NSMenuDidBeginTrackingNotification notification, but it's hard to tell. For example, this will fire for keyboard-initiated tracking, too.
Go to MainMenu.xib and look at the menu layout. It will be something like
Main Menu
Menu Item - File
Menu - File
Right click on The NSMenu you want to observe, not the NSMenuItem. attach the delegate to an object or controller of your choosing.
Then implement
- (void)menuWillOpen:(NSMenu *)menu

How can I get the element that was right-clicked in a context menu on a NSOutlineView?

I have a NSOutlineView and also created a menu that shows up as context menu when I right-click on any element.
Now I have 2 problems.
How can I identify the element that was right-clicked in order to act accordingly on any function in the context menu.
How can I alter the context menu, before I actually open it to remove or disable an menu item depending on the element that was right-clicked?
The clickedRow method (inherited from NSTableView) will give you the row number.
The row number can then be transformed to the item using NSOutlineView's itemForRow.
For the menu part, the NSMenuDelegate method menu:updateItem:atIndex:shouldCancel: is called just before the menu is shown, so you can modify it as needed.
According to the docs, you also have to implement numberOfItemsInMenu:.
You can set the same object as delegate for both the outline view and the menu.
In theory, when a row is right-clicked, it should be already the selected row in the NSOutlineView. Anyway, this does not happen normally.
I have solved this, by implementing the following method (that also answers to the second question):
- (void)menuNeedsUpdate:(NSMenu *)menu
Insert this method in one of your classes and register that class as the menu delegate (please note: the NSMenu, not the single NSMenuItem). In the above code, I have added this piece of code, to automatically select the clicked row when is right clicked.
if ([[arrayController selectedObjects] count] == 0 || [[arrayController selectedObjects] count] == 1) {
if ([myTable clickedRow] != -1) {
[myTable selectRowIndexes:[NSIndexSet indexSetWithIndex:[myTable clickedRow]] byExtendingSelection:FALSE];
}
}
As far as I know, this works correctly inside an NSTableView, so it should work also in your NSOutlineView.
Regarding the second question, you can use the above method to get notified every time the NSMenu is going to be displayed.

How to determine the current active view in tabbarcontroller

I have a tab bar with three tabs. In one of the viewcontrollers belong to the middle tab, my code needed to determine if the active view is the first viewcontroller's view. Any idea?
You can tell which view controller is the active one by calling the selectedViewController property of your app's UITabBarController(documentation linked for you).
There's also a selectedIndex property as well.

Objective C: How to reload tableview immediately after switching tabs?

I have 2 tabbars in my tabbar controller. I am currently in the second tabbar, after I click on a 'done' button, the tabbar controller needs to switch to the first tab and auto refresh the tableview in it.
I am able to execute on the first part
//Switch to the first tab's view
self.tabBarController.selectedViewController
= [self.tabBarController.viewControllers objectAtIndex:0];
However I need some advise on how to refresh the first view's tableview immediately after the switch is made. Any advise is appreciated.
EDIT: Updated requirement
Note: I do not want the view in the tabbar to reload everytime someone clicks on the tab. The reload should only happen in tab 1's tableview when
1) User is in tab 2
2) User clicks on 'Done button' in tab 2
3) App switches to tab 1 + Reloads the tableview in tab 1
Rationale is that the user will update some information in tab 2 which can only be viewed in tab 1. I want the user to be able to view the updated info immediately after switching to tab 1.
You could just do [tableView reloadData] in the first view's viewDidAppear: method?
EDIT: Editing to add additional data.
General caveat here, Apple tends to frown upon having custom UI Patterns like this. I'm not sure what your app is trying to do here, but you should mostly stick with switching tabs only if the user explicitly clicks on that tab. Have you looked at modal view controllers if you want to present a screen and get some data back, instead of having it in a tab bar?
If not, I guess you could have a solution where the first tab is a delegate of the second tab. When the button is clicked, you could call [delegate doneBtnClicked:] so the control shifts to the first tab. In that function, you could use the setSelectedIndex: to your current tab and call reloadData: on the table view.
You can send NSNotification after
pressing done button on second tab
and handle this notification on first tab controller.
So, create a DONE Button as a UIBarButtonItem, and in it's IBAction, do the following.
For reloading the tableView for the view in the first tab, put that tableView as that class' property , and reference the class like #class class1 , and create an object as a property class1 *object1 for View1 (Tab1) in the class for View2 (Tab2), and access the array1 using object1 like
[class1.object1.array1 reloadData];
And for bringing the view to tab one, use
self.tabBarController.selectedIndex = 0;
Thats pretty much it !
Alternatively you can also use a singleton class for this purpose.

Putting an NSPopUpButton in an NSToolbar

Having wrestled with NSPopUpButton in a previous question here I am now trying to place a NSPopUpButton inside an NSToolbar. Essentially I want to create something similar to what XCode has by default on the left hand side of it's toolbar. E.g. a pop up button with an action button next to it.
I have seen a method that show's a programmatic way of creating an NSPopUpButton and then adding it to an NSToolbar, but then I can't work out how to do all the Binding stuff that was so handy last time.
Interface Builder hasn't been very helpful, so any help gratefully received.
P.S. Could I solve this by creating a custom view (containing an NSPopUpButton with the usual bindings) and then adding the custom view to the toolbar?
It's actually pretty easy to do what you want here. In Interface Builder, switch to the tree view (the second button on the View Mode segmented control). Expand the window and the toolbar. Then, from the library, drag a popup button onto the toolbar. Interface Builder will embed a new popup button in a custom view for you automatically.
To actually put the button on the toolbar, double click on the toolbar in the window. This will bring up the customization sheet. You can drag the popup button to the desired location on the toolbar.
If you wanted to do this programmatically, you would create a custom view containing your popup button. Then, you'd need to assign it to a new outlet so you can refer to it programmatically. In the toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar method, you would create a new NSToolbarItem per usual, and call setView: to assign the custom view to the toolbar item.
Old post, but note you can also double-click the toolbar in the window and drag straight into the "Allowed Toolbar Items".
You will want to open up the toolbar view anyway so you can drag things in or out of the default toolbar items.