MenuItem disabled after turning off the view - objective-c

I set up a project with two view controllers connected to two xibs (MainMenu.xib and MasterVC.xib)
In my MasterVC I programatically add NSMenuItems with actions (located in MasterVC) to a menu, that is located in MainMenu.xib (and is connected to AppDelegate).
let menuItem = NSMenuItem()
menuItem.title = object.name!
menuItem.keyEquivalent = object.shortcut!
menuItem.representedObject = object
menuItem.action = "testSelector:"
appDel.menu.insertItem(menuItem, atIndex: 0)
func testSelector(sender: NSMenuItem) {
let object = (sender.representedObject as! MyNewObject)
print("Name of set:", object.name)
}
My added menu items work (are enabled) as long as I have window from MasterVC opened. As soon as I close it I can't click those menu items (they are disabled).
Is there a way to keep them enabled all the time?

You need to do two steps in your storyboard or XIB file (whichever contains the main menu):
1)
Use the Attribute Inspector after selecting the menu and then turn off "Auto Enable Items" checkbox:
]
2)
And for each menu item you want to have enabled, select that menu item and use the Attributes Inspector to make sure this checkbox is set to on:
]

Related

Get the visibility status of a toolbar according to Customize perspective dialog in Eclipse

I'm working on a plug-in, which contributes to the toolbar. The toolbar is listed in the Customize perstpective dialog. When I uncheck the check-box, the toolbar disappears as expected. But when the toolbar is updated from my code (using the toolbar manager), it gets displayed again. I need to get somehow the visibility status from code in order to prevent the toolbar from beeing updated when disabled.
Do you anybody have any idea, where the Customize perspective dialog stores the visibility status of menus and toolbars and how to get it from code, please?
You can get information by item ID this way:
private static boolean isToolbarItemVisible(String id){
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
return !((WorkbenchPage) workbenchWindow.getActivePage()).getHiddenItems().contains((ModeledPageLayout.HIDDEN_TOOLBAR_PREFIX + id + ","));
}

How to show a Safari Extension popover programmatically

I'm trying to write a Safari extension that consists of a button on the main toolbar with a popover tied to it, and a contextual menu item. The basic feel is modeled after the feel of the 1Password extension.
One of the jobs of the popover is to allow a person to log in. I'm also conditionally changing the action of the contextual menu item, and if a who person isn't logged in clicks the menu item I would like to show the popover allowing them to log in, but I can't find a way to do this in the developer guides.
How do I "show" a popover?
If you only have one toolbar item and one popover (and never plan to add more), then it's just one line. Assuming you've already assigned the popover to the toolbar item in Extension Builder, you can just use:
safari.extension.toolbarItems[0].showPopover();
But if you have more than one popover and (potentially) more than one toolbar item, here's a generalized function to open a popover, specified by its identifier, under the specified toolbar item in the active browser window:
function showPopover(popoverId, toolbarItemId) {
var toolbarItem = safari.extension.toolbarItems.filter(function (tbi) {
return tbi.identifier == toolbarItemId && tbi.browserWindow == safari.application.activeBrowserWindow;
})[0];
var popover = safari.extension.popovers.filter(function (po) {
return po.identifier == popoverId;
})[0];
toolbarItem.popover = popover;
toolbarItem.showPopover();
}

Connecting Multiple NSMenuItems with Actions and State Variables

I'm not sure how to describe what I need but I'll give it a try, via an example :
Let's say we have a window and a sidebar, and want to toggle it (I mean the sidebar : on/off).
Now, let's also say that :
The user may toggle the sidebar via an item at the Main menu (e.g. Show Sidebar / Hide Sidebar)
The user may also toggle the sidebar via a button
And there is also another item, in some other menu, to do the very same thing (Show/Hide Sidebar)
What would be the most practical Cocoa-friendly approach to achieve that?
Of course, that means that, e.g. :
When somebody clicks the button, apart from the sidebar (showing or hiding), the menu items must now be showing the current status of the sidebar (e.g. "Show sidebar" must now turn to "Hide Sidebar" in all possible instances within menus, etc)
I hope you get the idea; it's definitely not something difficult; but I'm definitely confused on how I could use all of Cocoa's tricks to do it fast.
Thanks!
I'm assuming you have some controller object which implements an action -toggleSidebar:, and that both menus target the same controller. Also, in the controller, you keep an instance variable BOOL isSidebarShown.
Make your controller implement the NSUserInterfaceValidations protocol. Something like this:
- (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem
{
if (anItem.action == #selector(toggleSidebar:) && [anItem isKindOfClass:[NSMenuItem class]])
{
NSString* title = isSidebarShown ? #"Hide Sidebar" : #"Show Sidebar";
[(NSMenuItem*)anItem setTitle:title];
}
return YES; // either way, the menu item is enabled
}

Pushed icon in toolbar

I put on bevel button in toolbar, set image for this button and uncheck checkbox bordered. How make pushed icon in toolbar like this?
You need to mark your toolbar item as Selectable. Edit your nib file in Xcode, double click the toolbar, click the item in Allowed Toolbar items and, in the Utilities panel, show the Attributes inspector and mark the Selectable checkbox.
Return your item identifiers in the delegate of the toolbar:
- (NSArray*)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
{
return _toolbarIdentifiers;
}
The toolbar will consider them selectable, so the selected item will be displayed the way you need.

How can I set the default state of a UISegmentedControl?

Is there a way to set the starting selected segment in a UISegmentedControl in Interface Builder, or do I have to do it in the code? If it's in the code, is viewDidLoad the best place to set it?
From code, you can do:
self.segmentedControl.selectedSegmentIndex = someDefaultIndex
Whether you should set it in viewDidLoad: or not depends entirely on the structure of your application. For example, if your app is starting up and loading the view for the first time and needs to set the control to whatever value it had during the previous run of the app, then it definitely makes sense to do it there.
In Interface Builder when you select UISegmentedControl object on your UI, then in attributes pane, in segment control there's segment drop down menu, select segment that you want selected (0,1 and so on) and tick the 'selected' option below it.
Select default value for your UISegmentedControl via storyBoard
Open ur storyboard and select the UISegmentedControl
Select atributes inspector of your UISegmentedControl (in the right corner)
Search the 'segment' atribute and open de dropdown.
Select the item you want to be selected by default.
Mark the selected checkbox to set selected by default.
If you don't use storyboards and want to set a default index after some setup/networking like me, this little snippet will select something if the user hasn't. I placed this in my subclass of UISegmentedControl, but you could place this anywhere. (Swift 3)
Decl: var UISegmentedControlNoSegment: Int { get }
Desc: A segment index value indicating that there is no selected segment. See selectedSegmentIndex for further information.
Short version:
if selectedSegmentIndex == UISegmentedControlNoSegment {
selectedSegmentIndex = initialIndex
}
Longer version
func reloadData() {
guard let numberOfItems = dataSource?.numberOfItems() else {
return
}
removeAllSegments()
for index in 0...numberOfItems {
insertSegment(with: $image, at: index, animated: false)
}
if selectedSegmentIndex == UISegmentedControlNoSegment {
selectedSegmentIndex = initialIndex
}
}
After clicking the Segmented Control go to where you created the segments and choose the one you want be default. Then below that there will be a Box with "Selected" by it. Select that and it will be default.