how can I show only number format input in UITextField? - iphone

I am making simple application that will take hotel name and price of item, and I want to show the price in only number format, because it can not be in alphabatical format, so what should I do add in code so that only number will be inputted in the Price UITextField?
If you are not getting my question, you may ask anything again
I do appreciate if I will get proper way of doing this,

If you want your text field to accept only the numbers, you can set it's keyboardType property to UIKeyboardTypeNumberPad.
[yourTextField setKeyboardType:UIKeyboardTypeNumberPad];
There are different types of keyboardTypes available, which you can refer in UITextInputTraits Protocol Reference.

This could be done in the Interface Builder as well:

Related

UIPickerView custom title

I have an array that I use to populate my pickerView, a simple string array. The issue is that I need the first item to be something like "Choose team".
The only way I have achieved that is the add the first item in the array to be "Choose team", but this messes up the array structure for me and I wonder if there is another way of doing this.
So, can I add a default value to a UIPickerValue, if no: how would you have solved this issue?
There is no easy way to add a default value. You can duplicate the array and add the string to the duplicated version for display but then you have to be careful about indexing issues. If the array's value changes programmatically, it can be a disaster. This duplication thing is not a good design anyway.
Alternatively, you can add a fixed label on right side of each picker values. Here is the example
Fixed labels in the selection bar of a UIPickerView

change UITextField accessibility description

Is there a way to set the accessibility label of UITextField to be something other than the "text field". So instead of calling "text field", I want to name it "code verification field".
My advice would be to not try to outsmart the system on build-in Voice Over outputs.
"Text field is editing" is to a blind user the audible equivalent to "This item is selected and there is a blinking cursor in it".
Same as you are unlikely to remove/change the blinking cursor from your UITetxtField you shouldn't try to remove/change that built in output.
Feel free to use the accessibilityLabel and accessibilityHint to add more context to this field.
How to Quiet "Text Field" & Options to Replace It
I posted a related answer that illustrates a few ways to stop VoiceOver from announcing "text field" for a UITextField.
This is the a code excerpt from that answer.
Quieting "Text Field"
let textField = UITextField()
textField.accessibilityTraits = UIAccessibilityTraitStaticText
Replacement Text
You could use the accessibilityHint for the alternate control name you want "code verification field"
textField.accessibilityHint = "code verification field"
accessibilityLabel would also work. The practical difference is that accessibilityLabel is read first, then textField.text (the text entered in the UITextField), then the accessibilityHint.
The following is read as: "label, text, (short pause...) hint"
textField.accessibilityTraits = UIAccessibilityTraitStaticText
textField.accessibilityLabel = "label"
textField.text = "text"
textField.accessibilityHint = "hint"
You can find out more about UIAccessibility Element properties in Apple's API reference.
I believe you have to set the field's accessibilityLabel property (part of the UIAccessibility protocol). Perhaps you also have to play around with the accessibilityTraits property to override the UIKit labeling.
As per my knowledge, Voice over reads the accessibilityLabel, accessibilityHint, "text field is editing" when it is a textfield. There is no way to change this until you change the trait.
YOU can change the trait to UIAccessibilityTraitNone, so that it does not read as "textfield is editing".
Updating this question with some current advice.
It is not appropriate to override UIKit's default accessibility handling beyond the accessibility properties it provides. Use accessibilityLabel, accessibilityHint, accessibilityTraits, etc.
UITextField has built in accessibility and handles role and state information to provide a consistent and robust experience to VoiceOver users. You can suppress certain VoiceOver announcements using accessibilityTraits, but that does not mean that you should.
The most appropriate thing to do in such a situation is to simply set a suitable label:
myTextField.accessibilityLabel = "Code verification"
This will result in VoiceOver announcing, "Code verification, text field, is editing", which is what users will expect to hear.
Note that accessibilityHint can be turned off by users, so this property should not be used for essential messaging or information.

Entering text-data into a Table cell, without having a UITextField in each cell

I would like to let the user fill in some data to be submitted.
So I have a table with 7 cells, each of them are labeled so they know what data goes into the field.
I though about putting a UITextField in each cell, but it looks like its out of place and the user can enter up to 255 chars, so also it doesn't display the data so nicely.
Can anybody recommend a good way to handle this kind of thing, whats the best solution in your experience?
Maybe hiding the UITextField after they are done entering data and display the data in some other manner?
Kind Regards,
-Code
You can customize a UITextField in several different ways to make it not look out of place to you: things such as location, size, alignment, font, font size, background color, and etc. You can have the text field appearance change depending on whether the text field is in the selected table row or not.
You can also have a UITextField delegate pre-check any changes to the text field, and prohibit entering a text length greater than some number of characters, or illegal/unwanted characters, etc.

iPhone: UITextfield validation

I checked the existing examples here in S.O, for my case which one do you think is better,
There are many textfields in one view and I want to validate them just when the user is editing that particular textfield, cause otherwise I have to validate them together when user press next on the screen than I have show all validation messages which I dont want to bother with. I want to validate the textfield in 2 different ways, if its a number input I want to validate it in a defined range e.g if number between between 5 and 1000 or if its a text then if the lenght of the text is in a defined range e.g between 2 and 10 characters. And the user must be able to enter any input out of range.
Which is better;
Using textField:shouldChangeCharactersInRange
or something like this:
[textField addTarget:self action:#selector(validateField:) forControlEvents:UIControlEventEditingChanged];
And either the case how can I check on runtime that the number entered is in range and dont allow user to enter bigger or smaller numbers
Good one.
The answer for your question is
1) Set the tag values to all the textfields.
2) Use UIControlEventEditingDidBegin and trigger a method to validate the textfield before the currentone.
[textfield1 setTag:1];
[textfield2 setTag:2];// make sure to set tag values incrementally
[textfield2 addTarget:self #selector(validate:) forControlEvents:UIControlEventEditingDidBegin];
-(void) validate:(id) sender
{
// 10 is the number of textfields, change it accordingly.
if(sender.tag!=1 && sender.tag!=10)
{
//validate textfield with tag sender.tag-1
}
else
{
//handle first and last textfield here
}
}
Try this, probably this will solve your problem.
Actually, both of your approaches will be just fine! Using the delegate of the UITextField to call textField:shouldChangeCharactersInRange and perform the validation there is an equally good approach as using Target & Action. In both situations, you define a handler that receives the calling UITextField as an argument (one implementation of this is what #Anil Kumar suggested in his answer to tag the text fields. but this is actually not even required, you can just call isEqual: on the text fields directly to check for equality).
Personally, I tend to use delegation for these kinds of tasks though.
Update: If you want to go really fancy on this problem, you'll use Reactive Cocoa. The April Tech Talk of the Ray Wenderlich Gang had a great session on it and also discusses the issue of from validation, so it'll just match your case! :)
Take a look at TextFieldValidator developed in swift

How to limit the length of text/numbers in textfield on iPhone?

I want to limit the length of a textfield, so that a user can know how many characters he can input. But I find no such attributes of textfield on iPhone. Any ideas how to do this?
And how to switch from one textfield to another when it reaches its max length? (to let users type easily)
You might check out the UITextFieldDelegate protocol, specifically textField:shouldChangeCharactersInRange:replacementString:. You could check if your text field has reached it's maximum size, and if so move to the next appropriate text field by sending it becomeFirstResponder: