Placing a text field in a Menulet in Mac OS X - objective-c

I'm working on an menulet-based application for the Mac that requires me to have a text field in the menu. I've searched high and low and have not found any examples of how to do this, yet I have seen many menulet apps that implement a text field. I've found no way to do it from Interface Builder, so I guess this text field would need to be defined and added to the menu programmatically.
If anyone could help me with this issue, it would be greatly appreciated.

An NSTextField is a view based subclass, therefore in code, when you create the NSMenuItem you must use the – setView: property and add the NSTextField to the menu item.
Also if you use an NSStatusItem, it also has a -setView: property.

Related

Using multiple windows with Storyboards (Mac OS X development)

I have two window controllers (with their own view controllers) on a storyboard.
In one window, I have the main program, a basic text editor with an NSTextView. In the other window, I have a single button.
I found out how to get the window to display by linking it to a menu item. It works.
The main window is linked to my ViewController class by default. The second window is also linked to the ViewController class and has its button linked to an IBAction in the ViewController class.
I have some simple code in the IBAction that basically tells the NSTextView to change its font size to a much bigger font. I have confirmed that the code itself works when called in other methods.
The button works, BUT it is using an entirely different instance of my ViewController class. So in result: the text size doesn't change.
So my main question here is how do I get an IBAction in one window to affect an object in another window.
I hope I did an alright job at explaining myself. Keep in mind this is my first Stack Overflow question:) I tried my best to research this question but mostly found information on iOS development and using XIB files.
It sounds like you have two windows with the same controller class but want what happens in one window to affect the other window. The easiest way is going to be with notifications. When the button is clicked in one window a notification gets posted that all instances of ViewController receive and respond to by changing the font size as needed. You could also look into setting a user default when the button is clicked and using bindings to keep the text field's font size tied to the current default.

Textfields in different Windows update to same Value

I am new to Cocoa Programming.
So this may be an easy to solve Question.
I am playing around with Cocoa Bindings at the moment. And have a simple set up.
I have two windows with separate .xib files and WindowController Classes.
Each window has a TextField that is bound to the same Class/Object, that contains an NSString.
If I change the value on TextField in the "first" window, nothing happens and the TextField in the "second" window is not updated.
If I put a label in the "first" window, and bind its value all is working fine, and the labels value change when I change the TextField in the same window.
How do I get the TextField in the second window to update itself, when I change the Value in the first windows TextField?
The only way I get this to work is when I bind the TextFields to NSUserDefaultsController. But storing the data is not what I am looking for.
I was searching the web, but could not find a proper solution, so any help is welcome.
Thanks!
And now as answer.
Use the same shared window controller.
That will easily enable both windows to be different views of the same data model.

iOS text popup with clickable links

I'm looking for a simple solution to presenting popup text in an iOS app that contains text with hyperlinks.
At the moment, my text pops up as a UIAlertView. The user has a 'Close' button below which dismisses the box. However, this class (UIAlertView) doesn't allow the use of hyperlinks within the message text. I understand that creating a whole new custom UIAlertView is frowned upon (not to mention probably overkill for what I want to achieve).
Perhaps I'm barking up the wrong tree by using UIAlertViews. I'm new to iOS and don't know the scope of what's available. Essentially, I need a dialog (or window) to pop up, containing a string of text, a (close/back/dismiss) button, and possible hyperlinks within the text. Those hyperlinks in turn launch other popups/windows/dialogs of their own.
What I'm working on here is a simple dictionary application. It's a table view containing terms. The terms lead to definitions, and in most cases, the definitions themselves reference other terms. Fairly simple, and if possible I'd like to use standard API classes.
Any wisdom would be appreciated. If I truly have to go down the route of creating my own custom UIAlertView class, then sobeit! In this case, some pointers for lightweight class creation would be appreciated. I'm not looking to add fancy colours or anything, just the ability to click on bits of text.
I think you'll need to create your own UIView sub-class to do the trick, controlled by a UIViewController subclass. One trick I've used to make it look more like an alert view is to have your main popup view embedded in a fullscreen view with a clear background.
In general, Apple SDKs make it really easy to do standard things (UIAlertView), but if you want to tinker with it (embedded links), you need to do it yourself.

Is assigning the same Action-method to multiple Cocoa UI objects e.g. NSButton possible?

I'm currently learning ObjC and Cocoa programming, coming from the Java world.
To test my current skills and learning progress I'm creating a small calculator app from scratch (OSX not iOS).
My UI has 10 digit buttons 0-9 among others.
My first thought was, since the action receives the senders reference, to make one action
like -(IBAction)captureDigit:(id)sender and then just grab the digit from the button title.
But the interface builder only allows an action to be connected with one sender it seems.
So I ended up creating 10 captureDigit actions in my controller.
My Question:
is the first option possible somehow? I thought of adding the actions programmatically (is this possible?) to the buttons, but then I would have to add all digit buttons as outlets to my controller.
Bonus Question:
can a NSButton hold some kind of non visible value? Could not find this in the documentation.
Maybe this would violate the MVC pattern as the UI would then know of application specific data?
Thanks for any useful and kind answer in advance, I'm still learning
You can connect many senders to one target/action if you Control-drag from senders to the target, so that's not a problem.
WRT your bonus question, any NSView has an integer tag which you can set in Interface Builder. That's a convenient way to differentiate multiple similar views.
You can definitely connect more than more button to a single action. Also, you can use the tag field of any object to give it a "behind the scenes" value.
It's perfectly possible to add as many actions to a single controller. How is Interface Builder preventing you from doing this?
You could have a NSDictionary instance in your controller, in which you could match NSButtons to whatever data you want.
To make it easy, in IB create one button and drag from NSButton to File's owner it then shows all of the methods that we can send to NSButton, then select captureDigit:. Now copy and paste the button change the title, copy and paste in IB keeps the connection and use tag field as costique, nitrex have already said.

Changing focus from NSTextField to NSOpenGLView?

I am developing an interface for an OpenGL Simulation on the Mac using Interface Builder. All OpenGL stuff is being written in C++ (which I know fairly well). Unfortunately I have no idea about Objective-C.
Basically, I have a few NSTextField displaying info on an object selected on the Screen. Using these textfields the user is the able to manipulate the object on screen and then there is a Save Button and a Restore Button (which either save the new values or restore the original ones)
I have this all working. My problem is when I enter data into an NSTextField the "focus" of the windows seems to remain on the NSTextField (blue border remains around it).
I use the keyboard to interact with items within the NSOpenGLView, I need to pass the focus back to the NSOpenGLView when I hit either the Save or Restore buttons.
Sorry if this is a very straightforward question
Thanks
David
Have you tried using NSWindow makeFirstResponder method to make your NSOpenGLView the first responder?
Just managed to get it working.
I had to add the line:
[[NSApp keyWindow] makeFirstResponder: MyOpenGLView];
to the end of the function being called when I click on either of my buttons.
(Thanks Julio for pointing me in the right direction)