iOS如何在按下发送按钮时重置文本字段

时间:2014-06-25 21:07:38

标签: ios email uitextfield uitextview

一旦按下发送按钮,是否有重置1 viewcontroller中的所有文本字段?

更新

尝试将此添加到我的应用中以重置文本字段:

- (IBAction)resetAction:(id)sender {
    for (UITextField *textField in self.view.subviews) {
        textField.text = @"";
    }
}

按下按钮时,它会崩溃应用程序并显示错误

"以NSException"

类型的未捕获异常终止

通过这份报告查看了其他人的问题,他们似乎忘记了删除已删除的元素。然而,我已取消所有内容的链接,甚至为此创建了一个新的应用程序,以确保它不是那个,当我按下按钮时它仍然向我抛出这个错误。

更新

.h文件:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    IBOutlet UITextField *textField;
}
- (IBAction)resetAction:(id)sender;

@end

.m文件:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController


- (void)viewDidLoad
{
    textField = [[UITextField alloc] init];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}



    - (IBAction)resetAction:(id)sender {
        for (UITextField *textField in self.view.subviews) {
            textField.text = @"";
        }
    }- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

3 个答案:

答案 0 :(得分:1)

为了获得更多灵活性和更清晰的代码,循环遍历self.view.subviews数组中的每个UIView,检查它是否是UITextField类。如果是,请将它们转换为UITextField并通过将其设置为空字符串来清除它的文本。

 for (UIView *view in [self.view subviews]) {
    if ([view isKindOfClass:[UITextField class]]) {
        UITextField *textField = (UITextField *)view;
        textField.text = @"";
    }
}

答案 1 :(得分:0)

执行此操作的非常简单的方法是控制+单击并将要清除的文本字段拖动到

@interface

@property (strong, nonatomic) IBOutlet UITextField *txtClearAfterSubmit1;
@property (strong, nonatomic) IBOutlet UITextField *txtClearAfterSubmit2;
@property (strong, nonatomic) IBOutlet UITextField *txtClearAfterSubmit3;

@end

然后这里是一旦调用它就会处理它的方法:

    - (void)clearThisTextboxInput
    {
        self.txtClearAfterSubmit1.text = @"";
        self.txtClearAfterSubmit2.text = @"";
        self.txtClearAfterSubmit3.text = @"";
    }

答案 2 :(得分:0)

尝试此解决方案:

使用以下方法[self UITextFieldClearAll]

进行呼叫
- (void)UITextFieldClearAll
{
    for (UIView *view in [self.view subviews]) {
        if ([view isKindOfClass:[UITextField class]]) {
           UITextField *textField = (UITextField *)view;
           textField.text = @"";
        }
    }
}