What does clear button set UITextField to? - ios

I have a piece of code that's reacting to a text field being typed in. I want to check when the .text of the field is empty (either backspaced all the way or cleared with the little round X button on the right).
If I check for .text == #"" my code works provided the user backspaced. It does not work if the user used the clear button.
What does the clear button set the text field's .text to? (I've logged it out and it looks blank to me if I print it between two other characters.)
Perhaps I need to check for more than just .text == #""? Perhaps .length == 0 or something else?
Thanks!

The "clear" button probably sets the text property to nil.
The best check whenever you wish to see if text is empty is to use:
if (someText.length == 0) {
// soneText has no text (or someText is nil)
}
This works for empty strings as well as nil values.

Related

Disabling enter button in iOS keyboard

I have an iOS keyboard question.
Say we have a UITextField (let's call it textfield) and a standard virtual keyboard, with the Enter key set to Send. We'd like to disable the Send key when UITextField is empty, otherwise enable it. We have set enablesReturnKeyAutomatically to YES on the text field. This works fine when we manually edit the text in the text field.
However, the Send key doesn't seem to work fine when we programmatically change the text. Our app will clear the text field by calling textfield.text = #"" once the Send key is hit and the text is sent. In this case, we expect the Send key to be disabled since textfield's content is empty. But no -- it appears enabled.
We want to disable the Send key in such a case. Please share your experience if possible.
Thanks!
I think the best you can do is to ignore the return event this way:
- (BOOL) textFieldShouldReturn:(UITextField*) textField {
return textField.text.length > 0;
}

UIPickerView Default Value is Null

I'm using a UIPickerView, and it has the first value chosen by default. When the user doesn't move the UIPickerView from this initial selection, however, the value selected comes back as NSNull. How can I make it so either:
The first value is recognized whether the user changes the selection or not.
The UIPickerView contains a blank value that I can check for in the first spot.
You can use
[_picker selectedRowInComponent:0]
nil means no one move the picker (you can make the first item blank or sth like "--" )
Reference : https://stackoverflow.com/a/27058363/471840

Code input text field

Is there a native UI control for code input text field, for example like Whatsapp:
No. To achieve this, they're almost certainly tapping into the textField:shouldChangeCharactersInRange:replacementString: method for their UITextField, selectively accepting and formatting user input to match the dash-if-empty approach.
Further, I'm sure they've subclassed the field; per your comments there isn't a blue cursor - which isn't standard for a UITextField.
No there isn't. Use a UITextField, fill it with dashes, keep track of how many characters the user has entered, and replace the dashes accordingly as the user types.
There's a 4-digit code input text field called CodeInputView written in Swift.
In the past I've added a UITextField to the view and set its hidden == true. Then I show/hide the keyboard by calling becomeFirstResponder()/resignFirstResponder() on it. I listen for text did change notifications and update a visible label with the value of the hidden text field.

How would you go about disabling a button as long as 2 text fields are empty in Objective C?

I have a view controller that controls 2 text fields and an array that displays in a table. How would I go about keeping a button disabled until the 2 fields have at least one character and the array is not empty. I am thinking about using cocoa bindings, however I can't seem to figure out a solution.
Currently my button is binded to
BOOL buttonIsEnabled;
I use that in a notification function in order to keep the button disabled, except the button will only reenable if I call that notification function.
-(void)controlTextDidChange
This means if i make a change in an array, the button wont reenable until I re-enter text. I can't seem to figure out an alternative solution. Any suggestions? Thank you.
One solution:
bind the two text fields two two strings (say text1 and text2)
in the text field bindings check Continuously Updates Value
Add this code:
- (BOOL)buttonIsEnabled {
return (self.text1.length>0 && self.text2.length>0);
}
+ (NSSet *)keyPathsForValuesAffectingButtonIsEnabled {
return [NSSet setWithObjects:#"text1",#"text2",nil];
}
Lastly bind the buttons enabled binding to buttonIsEnabled.
Because of the Continuously Updates Value text1 or text2 will change whenever characters are added to or removed from the text fields.
And the + (NSSet *)keyPathsForValuesAffectingButtonIsEnabled method will cause a Key-Value change notification to be posted for buttonIsEnabled whenever text1 or text2 change.
You can create a custom cell, give outlets to your 2 textfields and button.
Now on cell for row at index path after filling the textfields text values, put a condition checking the length of the textfields. If length is greater than 0 you can enable the button or keep it disable.
Along with this you need to put same code in delegate method (textFieldDidChange) of textfield. So that when ever new text is entered the button gets enable or disabled.

Getting values out of text fields with UIAutomation

I'm trying to understand how UIAutomation can be used to perform automatic application tests, so I've created a temperature converter app that can be driven from the JavaScript interface. Here's the UI:
The top text field has an Accessibility label of "Celsius", similarly the bottom field has "Fahrenheit" as its Accessibility label. I'm trying to drive it through this script:
UIALogger.logStart("Test -40ºC == -40ºF");
var window = UIATarget.localTarget().frontMostApp().mainWindow();
var celsiusField = window.textFields()["Celsius"];
var fahrenheitField = window.textFields()["Fahrenheit"];
var convertButton = window.buttons()["Convert"];
celsiusField.setValue("-40");
convertButton.tap();
var fahrenheitValue = fahrenheitField.value();
if (fahrenheitValue == "-40.0") {
UIALogger.logPass("-40C == -40F");
} else {
UIALogger.logFail("-40C == " + fahrenheitValue + "F");
}
This correctly sets the text in the Celsius field to "-40", and when the button is tapped, the app updates the text in the Fahrenheit field to "-40.0". However, fahrenheitValue has the value "Fahrenheit" (the name/label of the text field), so the test fails. I wondered whether this was maybe a timing issue, so put in a one-second delay after the tap, which didn't change the behaviour.
Is it possible to get the text out of the text field and compare it with the expected value?
Is it getting "Fahrenheit" because that's the accessibility label or because it's the placeholder text?
It looks like value() might return the placeholder text. One workaround could be to modify the placeholder value once the user starts typing to set it to be the text the user entered and then reverting it to Fahrenheit when they delete? I assume that even when you call setValue it acts as it would when the UITextField delegate methods get called?
You could add a label to your view, update it and read it out.
Try hooking up the Accessibility Inspector to show what UIAutomation "sees" in your app. It might be that it's reading some stale values because of the way you set the values in your app.
One way of refreshing the values is to send an update event:
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil);

Resources