macOS Topbar "Refresh" dropdown menu selection to cause WKWebView refreshing? - macos

I'm trying to figure out how to map a Topbar menu selection to run a function. That function, in this case, would be to force WKWebView to refresh itself.
I'm not at all sure where to start from.
Main.storyboard allows me to delete, modify or add Topbar dropdown menus, so I have created a Refresh entry to the File -menu.
I've mapped it to CMD-R - since that feels like the most intuitive shortcut to run for Refresh.
If I ctrl-click on the Refresh -item in the hierarchy in Main.storyboard - I can connect it to First Responder and select reloadFromOrigin.
After building the app, pressing CMD-R will refresh the WKWebView.
However, the Menu item itself is not enabled, it just shows as grayed out. After pressing CMD-R once, the menu item is enabled.
How do I make it "be enabled" at the start of the app?

Related

How to enable auto generation of Window MenuItems (e.g. Tile Window to Left/Right of Screen and Open file with checkmark) in macOS menubar

I am currently creating a macOS menubar for an app without using any interface builder (no XIB/NIB files), just pure code. However I was expecting some items to be auto-generated during the start-up of the app. Items like "Start Dictation", "Emoji & Symbols" under Edit menu were existing as well as the "Enter Full Screen" menu item under the View Menu. But when it comes to Window Menu nothing was automatically generated, only the menu items I've set in the code. Do I have to enable some flags or options when instantiating a Window NSMenu so it automatically generates those items? I am new to macOS development so I feel like I am kind of lost. Thanks in advance.
The Window and Help menus are a little special in that they have their own NSApplication properties, so you will need to set them to your menus so that the system will know what they are.
For example, if you just create a window menu and add it to the main, all you will get are the items that you have provided. If you also set it as the application’s windowsMenu, in addition you will get all the stuff for moving, tab support, etc.
Setting NSApp’s helpMenu is similar, where a Spotlight menu item is added to the menu.

Toolbar buttons are disabled by default for Lion Cocoa app

I've created an interface nib/xib file. The app is basically working, which is cool. The standard toolbar buttons look bad, they're glossy and raised, while most apps in Lion have the flat, inner bevel look. So I created some image toolbar buttons and put them in the toolbar and they look great, but they're all disabled by default.
Is there a way to give these the correct state in InterfaceBuilder or do I have to use code to give them the proper state.
Cocoa and Objective C are very unfamiliar so it would be helpful to me if I just knew what to search for. Most of my searching brings me results for creating custom buttons for iOS.
I figured this out, here's what you do:
In the nib/xib file, double click your menu bar to show the "allowed toolbar items" sheet. From here, choose your button that you've created. In my case I'll select a back button that I want to connect to a webview control. Ctrl click + drag from the button to what you want to connect it to and then make the appropriate connection. In my case I choose goBack from the WebView.
Once it has a connection it now becomes active.

How to load second view in Preference window cocoa

I am developing a cocoa application for mac. I have created a preference window with four buttons in toolbar. I am loading views on click event of buttons. Its working fine.
What I want to know is how to load another view on click of button in a view. Like in preference window of Safari, there is tab named with Privacy. And there is a button 'Details...' in Privacy tab. When we click on that button it shows a new view which shows a list of cookies.
Any Idea how to load view like view loaded on click of 'Details...' button???
There are several approaches, depending on what you're trying to accomplish. The easiest is probably to create the view you want, but make it hidden when you don't want it to be visible. Then when the user presses the "Details…" button, make it visible (and possibly expand the window if necessary) by calling [-NSView setHidden:NO].
Another way is to make the view a separate view in your .nib file, and when the "Details…" button is pressed, insert the view into the appropriate window using [-NSView addSubView:].
You could also create the view at runtime when the user presses the "Details…" button. That seems like a lot of work, though.

How can I get to the menu bar in my app in interface builder

XCode works in mysterious ways (at least to me).
I simply want to create a Preference pane in my app. When I run my app, the stock menu bar comes up (Apple, MyApp, File, Edit...) and the "Preferences" menu item is grayed out. It makes sense since I haven't started playing with it.
How on earth do I add/remove/activate/inactivate menu items? I'm not talking about adding anything new, simply using what should be there.
Thanks in advance.
NSMenu has "Auto Enable Items" enabled by default. That means if the menu item does not have it's action message hooked up, it will appear grayed out. So in your case, you would simply set the Preferences menu item's "Sent Action" to whatever action shows your preferences window. This can be hooked to some sort of showPreferencesWindow: method of your AppDelegate, or directly to the showWindow: method of a window controller.
To dynamically enable/disable menu items the best way is to implement the NSUserInterfaceItemValidation protocol which is excellently documented here
Edit: Your app's menu bar items live in the MainMenu.xib file. The menu bar appears as a "Main Menu" object on the left hand side (if you're using Xcode 4) Simply click on the items to modify them, and you can Ctrl+drag connections to and from them like any UI object.

update statusbar menu on click mac os x cocoa

I've been playing around with a statusbar menu app and what I'd like it to do is to update the content of the menu when it is clicked. I've been able to get everything working on the awakefromnib function and it works as expected. Now i just want to update the menu everytime I click on the status bar. This surely can be done, since it's done in the dropbox statusbar menu and many of the native app statusbar menus. I'm not able to associate a action directly with the click of the menu and I don't want to leave a loop running in the background cause I don't really see a need for it. Any ideas? An alternative would be to have an action associated to a menu item which does not cause the menu to get hidden again. Any help would be welcome.
Just set a delegate for your menu, and implement the -(void)menuWillOpen:(NSMenu *)menu method.

Resources