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.
Related
I've below form:-
I've created this form programatically.
The code for this is below:-
let textFiled = UITextField(frame:CGRectMake(87.0, y, 100.0, 20.0))
textFiled.borderStyle = UITextBorderStyle.Line
textFiled.font = UIFont.systemFontOfSize(8)
Now if I set tag for these text field then it'll store Int value. Which is very tough for me to manipulate this form.
I want to set name like IBOutlet for each text field. So that I can easily handle this form.
Is it possible to do it in Swift 4...
I want Your opinion please....
Similar to tag, we have
accessibilityIdentifier
in which you can add String values
you can just say
textFiled.accessibilityIdentifier = "name"
and get value using the below:-
textFiled.accessibilityIdentifier
UPDATE:
I suggest we never use the accessibilityIdentifier as it's for testing, for your case you should subclass UITextField and add a custom property to identify the text field
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;
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.
What is the best way to disable UITextField's 'return' keyboard key if input area does not contain any text? UITextField with enablesReturnKeyAutomatically property set to YES enables return key even if there are only spaces entered, but I'd like it to enable return key only when the text is not empty. any suggestions?
I don't know how you'd override the text input trait behaviour, but you could use the text field delegate method textField:shouldChangeCharactersInRange:replacementString: to prevent the user being able to enter spaces into an otherwise blank string. This would prevent any whitespace text being added, so the return key should not be enabled.
If you're not bothered about the return key actually being enabled, you could use textFieldShouldReturn:, again one of the text field delegate methods.
I would do something like the following
Create an extension to String and add a trim method (it will become handy in other scenarios too that's why I suggest an extension.
public func trim() -> String {
return stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
}
Make your class a delegate of UITextFieldDelegate and add the following fuction
func textFieldDidChange(textField: UITextField) {
textField.text = textField.text.trim()
}
Set the delegate of your UITextField to self.
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.