How to clear the Entered textfield when the user taps on the button?
swift
create the IBOutletCollections or add all your textfield to one array for e.g
@IBOutlet var storeAllTexts: [UITextField]!
on your button action method, call the following
storeAllTexts.forEach { $0.text = "" }
objective C
create the IBOutletCollections
@property (nonatomic, strong) IBOutletCollection(UITextField) NSArray *storeAllTexts;
on your button action
for (UITextField *getCurrentText in self.storeAllTexts) {
getCurrentText.text = @"";
}
for sample your get the SO duplicate answer
How do I clear my UITextField's text when I tap on it again?
I can think in three solutions for your problem:
Option 1
Clear the TextField everytime you touch it
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == self.numberToAdd) {
self.numberToAdd1.text = @"";
}
}
Option 2
Clear the TextField
when you touch it, only if you already calculated the number.
@property (nonatomic) BOOL calculateDone;
- (void)viewDidLoad
{
[super viewDidLoad];
self.calculateDone = false;
}
- (IBAction)calculate:(id)sender {
//sum label number and textField number
int sum = [self.label.text intValue] + [self.numberToAdd.text intValue];
//set label
self.label.text = [NSString stringWithFormat:@"%d", sum];
// sum done true
self.calculateDone = true;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == self.numberToAdd)
{
if (self.calculateDone)
{
self.numberToAdd.text = @"";
self.calculateDone = false;
}
}
}
Option 3
Clear the TextField
when you click calculate button
- (IBAction)calculate:(id)sender {
//sum label number and textField number
int sum = [self.label.text intValue] + [self.numberToAdd.text intValue];
//set label
self.label.text = [NSString stringWithFormat:@"%d", sum];
//reset textField
self.numberToAdd.text = @"";
}
You can also check the text entered in the TextField
, because you only wants numbers in the TextField
, right?.
@property (strong, nonatomic) NSString *urlRegEx;
@property (strong, nonatomic) NSPredicate *urlRegExTest;
//This Regex accepts negative and positive numbers
self.urlRegEx = @"((-)?[0-9]*)";
self.urlRegExTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", self.urlRegEx];
- (IBAction)calculate:(id)sender {
// if text is a valid Number
if ([self.urlRegExTest evaluateWithObject:self.numberToAdd.text]) {
int sum = [self.label.text intValue] + [self.numberToAdd.text intValue];
self.label.text = [NSString stringWithFormat:@"%d", sum];
}
}
You can download an example code here.
How to create TextField with Clear button using TextEditingController?
Instead of suffixIcon
, you suffix
. This way the clear
button will not be visible if textformfield
is not in focus and will display the icon
when you tap on the field. Also, when you will tap on the clear
icon after typing something, it'll clear the field. Working sample code below:
TextFormField(
controller: _firstNameController,
textAlign: TextAlign.left,
cursorColor: Colors.white,
onChanged: (value) {
},
style: TextStyle(color: Colors.white),
decoration: InputDecoration(
labelText: 'First Name',
suffix: GestureDetector(
onTap: () {
_firstNameController.clear();
},
child: Icon(Icons.clear)
)
),
),
Hope this helps.
Clear text field box when tapped on iPhone
I know you've already got your answer, but there is an alternative using UITextField's delegate method textFieldDidBeginEditing:
--
- (void)textFieldDidBeginEditing:(UITextField *)textField {
textField.text = @"";
}
SwiftUI: Add ClearButton to TextField
Use ZStack
to position the clear button appear inside the TextField
.
TextField("Some Text" , text: $someBinding).modifier(ClearButton(text: $someBinding))
struct ClearButton: ViewModifier
{
@Binding var text: String
public func body(content: Content) -> some View
{
ZStack(alignment: .trailing)
{
content
if !text.isEmpty
{
Button(action:
{
self.text = ""
})
{
Image(systemName: "delete.left")
.foregroundColor(Color(UIColor.opaqueSeparator))
}
.padding(.trailing, 8)
}
}
}
}
How to clear the text field automatically
Use this method,
If you want to manually clear the text field use this code:
textField.text = ""
If you want the text field to empty when you begin editing, use the delegate method below:
func textFieldDidBeginEditing(textField: UITextField) {
textField.text = ""
}
If you are using multiple text fields, use the method below:
func textFieldDidBeginEditing(textField: UITextField) {
if (textField == oneTextField) {
textField.text = ""
}
else if (textField == anotherTextField) {
// Perform any other operation
}
}
Is there a way to reset the value of textfield by clicking the button? [Swift]
you could simply do this inside your IBAction func
yourTextFieldOutlet.text = ""
Related Topics
Why Is the Tab Bar Disappearing
How to Increment the Filename If File Already Exists
How to Associate File Types With an Iphone Application
How to Return Value from Alamofire
Ios Upload Image and Text Using Http Post
How to Check If a String Contains Another String in Objective-C
Starting Iphone App Development in Linux
How to Find Out the Type of an Object (In Swift)
Get Location Updates For iOS App Even When Suspended
How to Check How Long Ago Was the App Last Opened
How to Find Current Visible Viewcontroller in Ios
How to Force Nslocalizedstring to Use a Specific Language
How to Use Nsjsonserialization
Ios 8 Uitableview Separator Inset 0 Not Working