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
Related
Is it possible to set a placeholder text for a Swift Picker? I've been trying to search for a helpful answer but I haven't been successful so far, so hopefully this will help in solving the issue :)
Currently when passing available list values to the Picker I also pass it a default value to start of with. However, this default value is treated the same as if the user picked it. What I'm trying to achieve is that the default value should be grayed out (like regular placeholders for standard textfields) and when the user opens the picker that value would 'dissapear' as default forcing the user to pick something from the list (but without losing the range) - so f.ex. if I have a picker for values between 1-200 and I set my placeholder to 100 the picker would Still show this value when you open it (to avoid scrolling from the beginning) but it wouldn't be directly taken as the target value unless the user actually selects it.
A Menu may be a better option than a Picker in this instance.
Menu("Label") {
Button("Menu Item", action: menuAction)
Button("Other Menu Item", action: otherMenuAction)
}
Menu Documentation
Im my application when the user selects a TextField the existing text gets automatically selected when the Focus changes to the textfield. How do I hide the blue selection handles that appear?
The reason I want to automatically select the existing text is so that the user can begin typing and the textfield will automatically overwrite the existing value instead of appending to it.
I figured it out. You can set the enableInteractiveSelection property to false.
I would like to give my iPhone App user the option of whether to use a picker (i.e., scroll wheel) or keyboard to input the value of a field. I'd like it to where the current value of the field effectively displays as isolated text (i.e., as a label, with the picker and keyboard hidden). But the user can change the field's value by either (A) performing a single tap on the label to reveal the keyboard so the user can type in a different value; or (B) perform a swipe on the label to reveal the picker so the user can user the picker to change the value. This will present a very clean interface in my opinion, without the clutter of the keyboard, text field, or picker unless the user changes the value. Help?
This control doesn't exist, but a possible solution would be to add a cell under the UIPickerView (assuming your using a table form) when the "Custom" option is selected and add a text field to that cell allowing custom input. Make sure you hide the cell if the UIPickerView value changes from "Custom" to avoid user confusion.
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.
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.