NSTextView - How to detect when it is clicked? - objective-c

I'm trying to do this:
- (IBAction)textFieldSelected:(id)sender
{
printf("clicou no text\n");
}
I "connect" this to my NSTextField through the Interface Builder.
When I first start the app, the this NSTextField is already focused. Then I click on a second NSTextField, and my first one lose the focus and I get the print statement. Clicking back and forth between theses NSTextField I see that the print statement is just called when I click on the NSTextField that it is not attached to it. I believe that it just happens when the first one loses the focus.
Q1: How do I do to have this print statement when the use click on the NSTextField (when it gets the focus)?
Q2: How do I avoid it to get the focus automatically when the app starts?

Create the custom class of NSTextfield and then implement below method, so that whenever focus goes to the textfield below method will get called:-
-(Bool)becomeFirstResponder{
return YES}

Related

UITextField losing focus after button press and text field switch (keyboard remains visible)

I've got a UITableViewCell subclass here that manages a UITextField setup on the right side of the table view cell. Anywhere from 4 to 8 of these cells are displayed at any given time depending on the table; I use them for unit data entry (ie, entering in distances, temperatures, etc) so there's quite a bit of logic bolted to the cell subclass.
For whatever reason, I've noticed that if I perform the following steps:
1) Tap on a text field to begin editing and bring up the keyboard
2) Enter in some text
3) Tap the clear button (which is enabled on the text field)
4) Tap on another textfield in the same table view
Then the current UITextField loses focus, but the second text field does not gain it. This means that no UITextField currently has focus, but the keyboard is still being displayed on-screen... but without any active text field, it does nothing, and cannot be dismissed (presumably because there's no first responder to resign?).
I can then tap on another text field again, and it will take focus and begin editing- at which point the keyboard becomes operable again and pressing the return/done key will dismiss it and end editing as usual.
If I simply tap on another UITextField without first hitting a button, then the second UITextField will gain focus immediately (as I'd expect it to). But it seems like button presses outside of the UITextField will cause this behaviour to occur if you try to switch fields after tapping any kind of button other than the text field.
Does anyone know what is causing this? It almost sounds like there's something wrong with the responder chain, but I'm not sure what the problem would be or how to fix it.
Firstly,you are sure the textfiled in the table has a unique identifier ,such as tag.
Secondly,you should make another textfiled become first responder if you want a textfiled lost first responder but the keyboard still appear.
Figured out what it was...
The problem was that I was reloading the table data in the delegate method that my custom cell was calling upon edit completion. Apparently reloading the tableview data while you're in the middle of switching UITextFields will cause the second text field to not gain focus (but the keyboard won't get dismissed), hence causing the issue I was seeing.

NSTextField and FirstResponder

I am developing a cocoa application that has a main window and several panel windows.
I need to autosave some text on a NSTextField (which is on a panel window) when user leaves the textfield by clicking the main window etc. so far I have tried out by implementing resignFirstResponder on a NSTextField subclass,however if I click on another textfield on the same window "resignFirstResponder" gets triggered but if I just click on somewhere empty on my main window it does not get triggered. (NSTextField loses the blue focus though)
I need to capture this event the NSTextField loses the focus ring to save the uncommitted changes . Any pointers would be highly appreciated.
This text field is on NSTableCellView
use [[NSApp mainWindow] resignFirstResponder];
How about using the NStextfield's action sent on end editing
Then simply right-click-drag to an object (or FirstResponder) in your Xib file and connect it to a method. It should now run this method when you end the editing (deselect, Enter or Tab).

NSTextField click-through?

I have a static NSTextField that overlays a large error message in my OS X app. I'm trying to get it to allow the user to click controls beneath it.
In IB I've unchecked "enabled" and I've checked "Refuses First Responder"
I've also done it in code because that wasn't working:
[largeErrorText setEnabled:NO];
[largeErrorText setRefusesFirstResponder:YES];
Still, it is getting in the way of interacting with the objects below it. Any ideas what else it might be?
The only way I have found to make an object transparent to the click is to subclass that object (in your case the NSTextField) and override the hitTest method returning nil. This way that NSTextField will not respond to the click so the NSView below will respond to the click.
- (NSView*)hitTest:(NSPoint)aPoint
{
return nil;
}
I assume you are describing a scenario like the following image shows:
The inner red rectangle is the frame outline of the NSTextField label, and you're saying that even though you've disabled the text field and set refuses first responder, your clicks do not go through to the NSButton?
This design scenario describes a condition called "Overlapping sibling views". I would generally try to avoid this if at all possible. If you can't, you can get the desired behavior by making sure that the NSTextField label is "behind" all of the other UI objects that you want to be able to interact with. You can do that by selecting the label and choosing Editor > Arrange > Send to Back. That will assure that the button is in front of the text field so that it can properly intercept mouse events.

Getting UIPicker to appear when user selects a UITextField

I have a simple UITextField called month where I get users to simply enter the month they want via the keyboard that comes up. I would now like it for them to be able to use a UIPickerDate (or UIPicker) to make this selection instead. So when they press on the text field, a mini UIPicker appears and they make there selection, press anywhere on the screen and the picker disappears.
Does anyone know how to do this or has any suggestions? I am pretty new to programming and have looked at other answers but everyone seems to be referring to this being done in a table.
Thanks in advance!
You can set the inputView property on the UITextField to be an instance of UIDatePicker. When the instance of UITextField becomes the first responder, the picker view will be displayed with the standard keyboard animation.
// Assume that self.monthTextField and self.datePicker
// are properties of the view controller class
self.monthTextField.inputView = self.datePicker;
As for dismissing, that depends on the context. If there are more text fields to populate, consider adding a UIToolbar as the inputAccessoryView of self.monthTextField. Then you can add something like a UIBarButtonItem to make the next text field the first responder, similar to how the standard keyboard provides a Next button.

NSTextField in status bar doesn't want to receive focus

For some reson sometimes a NSTextField I'm using in Status Bar menu doesn't always allow me to input text. I click it and nothing happens as if it was disabled. Upon restarting program it works again. I don't do anything with it, it's just created in the interface builder.
That's because no NSWindow contains the NSTextField. The NSWindow sets the first responder when the window gets the main window. The NSStatusBar is global. It's never focused so your textfield only will be focused in the very beginning.
I'm not sure if there's a way to solve this problem in a nice way. You might try to set the first responder manually. You could also add a global event monitor
Example:
[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent* incoming) {
[textfield setStringValue:[incoming characters]];
}];
Note: This is a very bad way to fix this problem. I'd first try to set the NSTextField manually as a first responder if this is possible.