Objective-C, auto erase NSTextField string - objective-c

I've been trying to figure out how to auto erase a string within an NSTextField upon clicking within in it.
For example the current behavior is like this on OSX:
When a user clicks in the cell or the default focus is the cell either the word is sometimes highlighted or the cursor is placed in the middle of the text. The user then needs to use backspace to delete the text (which I'd like to eliminate by automatically removing the text).
Is this a function that is built-in that I'm somehow missing? If not how would I go about it?

As far as I know there is no standard function to delete the text that is in the textfield. Depending on your controller class you can try to work with mouseDown or acceptFirstResponder, but I don't believe this will provide a stable situation.
I would recommend you to start with an empty textfield (setting in xcode) and empty the textfield after the content of the textfield has been processed. For instance:
-(IBAction)addYourTextFieldInWhateverYouWant:(id)sender
{
//copying the string from the textfield
NSString *text = [[self yourTextField]stringValue];
//now you can empty the textfield
[[self yourTextField]setStringValue:#""]
//Do here whatever you need to do with the input from the textfield.
....
}
It doesn't give you exactly what you want, but it provides the user with an empty textfield where the user can immediately type in his/her text when the textfield is clicked upon and the user doesn't need to delete any text in the textfield.
Hope this helps.
Kind regards,
MacUserT

There are plenty of terrible ideas “outside the box”, and this is one of them.
Anyway, make a subclass of NSTextField. Override becomeFirstResponder. If [super becomeFirstResponder] returns YES, set self.stringValue = #"".

Related

How can I make my NSTextField NOT highlight its text when the application starts?

When my application launches, the first NSTextField is being selected like this:
I can edit the NSTextField fine, but when I press enter to end the editing, the text becomes selected again, and the editing does not end.
I followed the Apple tutorial here, and I had the same problem with the text field being perpetually highlighted.
How do I stop this? I would like it so the text field is not the first responder of the app so it's not edited right away, and when it is being edited, clicking outside of the text field will end it. I'm not sure where to put the [[textField window]makeFirstResponder:nil] to stop the editing in the latter case.
I'm running Yosemite 10.10.2.
Your text field is selecting the text, due to the default implementation of becomeFirstResponder in NSTextField.
To prevent selection, subclass NSTextField, and override becomeFirstResponder to deselect any text:
- (BOOL) becomeFirstResponder
{
BOOL responderStatus = [super becomeFirstResponder];
NSRange selectionRange = [[self currentEditor] selectedRange];
[[self currentEditor] setSelectedRange:NSMakeRange(selectionRange.length,0)];
return responderStatus;
}
The resulting behavior is that the field does not select the text when it gets the focus.
To make nothing the first responder, call makeFirstResponder:nil after your application finishes launching. I like to subclass NSObject to define doInitWithContentView:(NSView *)contentView, and call it from my NSApplicationDelegate. In the code below, _window is an IBOutlet:
- (void) applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
[_menuController doInitWithContentView:_window.contentView];
}
The reason your field is getting focus when the application starts is because the window automatically gives focus to the first control. It determines what is considered first, by scanning left to right, top down (it scans left to right first, since a text field placed at the top right will still get focused). One caveat is that if the window is restorable, and you terminate the application from within Xcode, then whatever field was last focused will retain the focus state from the last execution.
I am using IB, there's a property on NSTextField called Refuses First Responder. Ticking that will prevent the highlighting of the text field immediately after the window is presented. There's some more detailed info about Refuses First Responder in this question.
No need to subclass. Simply set refusesFirstResponder = YES;
NSTextField *textField = [NSTextField new];
textField.refusesFirstResponder = YES;
That's it! Do that and it won't highlight the text in the field.

How to make NSTextfield lose focus when Enter is pressed?

I want to make a text field in which if I click out of it, or press enter, it makes it lose focus. As in the focus ring disappears. I've have seen situations like this, but I do not know where to place the code for it. Can anyone show me how to make the NSTextfield lose it's focus?
One method would be to implement the NSTextFieldDelegate, assign the delegate to your text field, and have it call a selector (a method in your code that changes makes the first responder = nil). The delegate will be called with a message when the text field is finished receiving input. Check out the API here for more information:
https://developer.apple.com/library/mac/documentation/cocoa/reference/NSTextFieldDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/NSTextFieldDelegate
For people out there as lazy as me, here is some code.
This is the cut down NSTextfieldDelegate method i used:
func control(_ control: NSControl, textShouldEndEditing fieldEditor: NSText) -> Bool {
DispatchQueue.main.async {
self.textField2.window?.makeFirstResponder(nil)
}
return true
}
https://developer.apple.com/documentation/appkit/nstextview/1807135-resignfirstresponder?language=objc
resignFirstResponder Docu says the following:
Use the NSWindow method makeFirstResponder:, not this method, to make
a text view the first responder. Never invoke this method directly.
// don't use this as the name may suggest (at least it did for me)
self.textField2.resignFirstResponder()

NSComboBox action selector fires when setHidden:YES

I have an NSComboBox. I've set an action selector. When the box gets hidden the selector fires, even if the user never touched it. Yes, I need to hide it.
IBOutlet NSComboBox *comboBox;
[comboBox setAction:#selector(onComboBoxSelection:)];
- (void)onComboBoxSelection:(id)sender
{
NSLog(#"Why does this fire twice");
//My code doesn't actually set hidden here, it's just for proof while debugging the issue.
[comboBox setHidden:YES];
}
Why would hiding an NSControl fire it's selector? What's the best way to fix it?
Update:
I've fixed it by wrapping the method. But I'd still like to understand why, or other ways to fix it.
- (void)onComboBoxSelection:(id)sender
{
if(![sender isHidden]{
NSLog(#"Now only fires once");
//My code doesn't actually set hidden here, it's just for proof while debugging the issue.
[comboBox setHidden:YES];
}
}
Set a breakpoint in onComboBoxSelection: and look at the backtrace when it's called the second time (type bt in the debugger to see the backtrace). That will explain what's going.
A combo box is both a text field and a popup, and it will fire actions for both. The text field action is fired when editing ends, either by hitting the Return key or when it resigns first responder (e.g., tabbing out to another field).
When you hide the combo box, the text field resigns first responder and fires its action.
What you probably want to do is check if combo box value has actually changed, and only then proceed with hiding the combo box, etc.
Another option is to use data bindings to observe changes to the combo box. Bind the combo box value to a property on your controller. Then implement the property setter in your controller.
try this [comboBox setHidden:1];

Prevents UISearchDisplayController from hiding the UiTable

Im new to objC and Im currently experimenting on UISearchDisplayController. Basically I have an array of string as my data and I use UISearchBarDisplayController to filter my data. Im able to retrieve the correct values when I enter my searchText into the search bar. However, the tableView disappears when my searhBar text is empty.
Would it be possible to prevent the tableView from hiding in this such case. What I want is to just to display all the values in my array in the table if the searchBar text is empty.
I checked the hidden/alpha/frame property of the table and tried to fix my issue here but I think Im in the wrong path here. I'm thinking if i need to subclass the UISearchDisplayController and override the [setActive:YES animated:YES];? Any hint would be appreciated.
You should recieve an event for any change to the search parameters, including when the string is empty.
If you change your implementation of that delegate method to check if the string is empty, you can return the original data instead of the filtered data. This should achieve what you're asking for without the need for subclassing.
I ended up working with UISearchBar and UITable.
So here are the scenarios I encountered.
Display the the UITable when the searchBar is clicked.
UITable shows all the data from my plist when searchText is empty.
UITable shows filtered results from my plist that matches the searchText.
Dismiss the keyboard on press of the search button but don't disable the cancel button on the searchBar.
Remove the UITable when cancel button is press.
I don't have the animation for showing the table for now but this works for me. Also, I allowed the user interaction and scrolling on my table during search so no overlay needed in this case. Glad its working now. :)
I created a sample project for those who might need. Here is the link. Apologies for a messy code and leaks issues. Just posted this to share. :)
You could try by always leaving a zero-space width characters in the search textfield :
static NSString* zsp = #"\u200B";
//In the UISearchBarDelegate
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if(searchBar.text.length == 1 && [text isEqualToString:#""])
{
searchBar.text = zsp;
return NO;
}
return YES;
}

UIButton highlight setting won't stick

There sure are a lot of UIButton questions here, and I was hoping to find the answer to this, but nothing quite like this particular issue.
I have a few buttons, and I can call button.highlighted = YES; for any button when the program runs and it shows up highlighted.
I thought I could then use this same technique to set a button's highlight state to YES after it pressed, and then set it to NO after another button is pressed. This way, the current selection remains highlighted.
For example:
-(IBAction) buttonPressed:(UIButton *)button
{
if (button.tag==1)
{
self.button1.highlighted=YES;
self.button2.highlighted=NO;
// do other program stuff here
}
if (button.tag==2)
{
self.button2.highlighted=YES;
self.button1.highlighted=NO;
// do other program stuff here
}
}
Even though the highlights work fine if I place the highlighted = YES; code inside viewDidLoad. But the above code does not work. The highlight doesn't stick. The buttons works, and does the other stuff it needs to do, but the highlights fail to stick.
I would think this should be pretty basic. Is iOS somehow automatically setting all button highlights to NO on its own after any button operation?
May be you should use other means to present the highlight status, because of the statement from UIControl's documentation:
By default, a control is not
highlighted. UIControl automatically
sets and clears this state
automatically when a touch enters and
exits during tracking and when there
is a touch up.
You might try using the 'selected' property instead of the 'highlighted' property.