Get the number value of a texfield Xcode - iphone

I want to have a textfield where the user can input a number and then set a label to that value. Is there any "texfield.value" or something that can fetch the value in numbers?
This is the only piece i heve done. should i put it here?:
- (IBAction)set:(id)sender;
{
}
Thanks in advance

In Xcode, select the textField or textView and change the keyboard type to Numeric.
Then you can get the value of whatever the user inputs into that field by doing:
int value = [[textField.text] intValue];

You can grab the integer value of a textfield using the following snippet.
[textField.text intValue]
Hope this helps.

You can get by textfield.text this will return the text value of that text field , now to convert it to number
NSNumber * mynumber = [NSNumber numberWithInt:[[textfield. text] intValue]];
nslog(#"%#",mynumber);

Here's an example of something I just used:
[self.phoneNumberTextField.text intValue];
You have to be careful with this one. Here are the steps to get this to work
In storyboard, select the textfield
Open the attributes tab for the textfield in Interface Builder
Change keyboard type to "Number Pad" (This guarantees that the user will only be able to enter ints into the textfield)
Now, in your code, type [self.myTextField.text intValue]; (set this equal to a local variable)
Now, as far as WHERE in the code you'll put this, it depends. If you're trying to log in a user, I'd recommend putting this in an IBAction like 'loginButtonPressed'. Or before you transition to a new screen in 'viewWillDisappear' or even 'prepareForSegue'.
Good luck!

This is from 3 years ago, I tried using that and didn't work and so I find this working for me
int i = textfield.text.intValue;

Related

Swift UIAlertController fire once per row select textfield edited

I have a UIPickerView with an if statement in selectedRowInComponent that checks the value of a textField. If the user inputed number in the textfield is greater than 10, an alert is called. All is working well except I'd like to have the alert only fire a single time after the row is selected and the field is edited. As it stands, the alert is called every time the field is updated/edited with a value greater than 10. The initial alert is sufficient. Any tips on how I can accomplish this? Thanks!
if (inputField.text! as NSString).doubleValue > 10 {
SweetAlert().showAlert("Number is greater than 10", subTitle: "Please select number less than 10", style: AlertStyle.CustomImag(imageFile: "alertimage.png"))
}
Quick and Dirty:
Create a bool variable publicly available in your viewController and initialize it with false (lets call the variable 'alertAlreadyShown')
Then just make an if statement about that bool in your pickerView method
if (inputField.text! as NSString).doubleValue > 10 {
if (alertAlreadyShown == false) {
SweetAlert().showAlert(...)
alertAlreadyShown = true
}
}
But don't forget to reset the bool at appropriate times to enable the alert again
There's two ways you could go about this. One would be (and the most user friendly in my opinion) only show values of less than ten in your picker view. The other option would be to set a bool for whether or not the alert had been displayed, and add that as a check to your if statement.

Automatic typing password iphone

In my UIViewController I have 2 textfields. One called passwordtextfield and the other retype. Instead of having people actually retype their passwords I want it to automatically fill it in. Is this possible? I tried something that keeps on crashing.
[_passwordtextfield addTarget:self action:#selector(updateTextField:) forControlEvents:UIControlEventEditingChanged];
- (void)updateTextField:(id)sender{
UITextField *retype = ((UITextField *)_passwordtextfield).text;
}
There is no need to cast your text field. You can assign direct value of your text field.So
Write this line
retypetextfield.text = _passwordtextfield.text;
instead of
UITextField *retype = ((UITextField *)_passwordtextfield).text;
I don't know why you want to do this, as mentioned in the comments, but to answer the question... It looks like you are creating a new UITextField in updateTextField instead of setting the text of one that exists. It should look something like this:
- (void)updateTextField:(id)sender{
retypeFied.Text = _passwordtextfield.text;
}
Substitute whatever you named the pointer to your second field for retypeField.

Get variable of Keyboard type

I have been trying to get the variable for the keyboardType
self.titleField.keyboardType
When i use nslog it returns about 10 values that are either 0 or 4
I have implemented custom behavior for the number pad keyboard so i need to check if it's the number pad or default keyboard that is showing up.
I have also tried to use self.view.tag to set it by view instead of keyboard.
The nslog for that returns all the tags of the previous view controllers that the application
has been through.
I think this may be because i'm reusing a single view controller class for each separate
controller. I am using a switch statements to determine what
data to display based on the tag.
Is their a way to reset the tag in viewWillDisappear?
Try assigning tag value to textfields and check which textfield became first responder in TF's delegate method -textFieldDidBeginEditing:
The UIKeyboardType enumeration gives names to the keyboard type numbers:
switch (self.titleField.keyboardType) {
case UIKeyboardTypeDefault:
NSLog(#"default keyboard");
break;
case UIKeyboardTypeNumberPad:
NSLog(#"number pad keyboard");
break;
default:
NSLog(#"unexpected keyboard type %d", self.titleField.keyboardType);
break;
}

Strange issues with tag label 0?

I have created a bar over the keyboard for textfields with previous/next/done button selections. In doing so, I noticed an odd occurance with my tags that I used to navigate between the textfields. I am creating my interface programmatically with a loop, and as such, just set the tag values to the loop variable i.
I started the i variable at 0 so the very first text field created had a tag of zero. Basically what was happening is the 'previous' button functionality would only go so low as 1. It wouldn't even go back to the text field with the 0 tag. The only way to fix this was to increase all tag values by 1 so the first text field started at 1 instead of zero.
Here is my code. Is there a bug in my code that I cannot see? or is this a weird issue with tags?
-(void)gotoPrevTextfield{
// If the active textfield is the first one, can't go to any previous
// field so just return.
UITextField *textField = (UITextField *)[inputsView viewWithTag:0];
NSLog(#"%i",textField.tag);
NSLog(#"%i",txtActiveField.tag);
if (txtActiveField == textField) {
NSLog(#"returning at previous");
return;
}
else {
NSLog(#"set responder");
// Otherwise if a different textfield has the focus, the operation
// of "previous" button can be done and set the previous as the first
// responder.
textField = (UITextField *)[inputsView viewWithTag:txtActiveField.tag - 1];
NSLog(#"%i",textField.tag);
NSLog(#"%i",txtActiveField.tag);
[textField becomeFirstResponder];
}
}
Note that unset tags default to 0 so that is almost a poor choice. You may be getting another view that you don't expect.
A fairly good practice is to add some constant such as 100, consider making the constant a const int or #define for clarity.

Custom iPad keyboard that looks like the system keyboards

I'm looking for a non-hackish solution for this, so basically -inputView. The part that I'm not sure about is how to make it look like the regular keyboards, from the background to the keys. I realize that I could photoshop an apple keyboard, but this seems like it is a little hackish, especially if apple (probably not but still possible) decides to change the look of their keyboards. I know Numbers has done an excellent job of making extra keyboards that look like the standard system ones, and I would like to do it like those (although obviously they have access to the same resources that made the system keyboards, including possible private frameworks, etc.)
I used the following:
tenDigitKeyboard.m
-(IBAction)pressedKey:(UIButton *)sender
{
[delegate pressedKey:sender.tag];
}
where delegate is defined as `id delegate;
then in the delegate i do...
-(void)pressedKey:(NSInteger)key
{
NSString * bufferString = model.string;
if (key == -1) {//delete
model.string = [bufferString substringWithRange:NSMakeRange(0, [bufferString length]-1)];
}else{
//will need to change the following to lookup key value based on a lookup of the button.tag
model.string = [bufferString stringByAppendingFormat:#"%i",key];
}
[self update];//updates the view
}
I got the keyboard button artwork from: http://www.teehanlax.com/blog/iphone-gui-psd-v4/
Create a view controller and xib. The xib should have 1-9,0 and delete buttons mapped to IBOutlets in your controller. Store and retain the return value string as a property. You can add decimals, etc. if you wish. In the header, store an edition block closure with a property (or alternatively create a delegate or use notification).
#property (copy) void(^valueChangedBlock)(NSString* string);
On touch up, each button sends an event to a method like this:
- (IBAction) pressKey:(id)sender
{
NSString *toAppend;
// Instead of this switch you can store the values in a dictionary mapped by sender.
switch(sender)
{
case oneButton: toAppend=#"1"; break;
case twoButton: toAppend=#"2"; break;
...
}
returnValue = [returnValue appendString:toAppend];
valueChanged(returnValue);
}
Obviously the delete key should remove a character from the end of the string instead of appending. Other than creating the controller and adding this view as the inputView, you should add the valueChangedBlock and set it to update the text field. You may want to put a clear custom button over the text field set to make the field first responder so it doesn't appear as if the user can edit at any point in the string.