我怎么知道,在UITextField和UITextView中是否进行了任何更改/修改?

时间:2017-10-27 05:59:47

标签: ios objective-c iphone textview uitextfield

这是我的观点,enter image description here

在此视图中,除“说明”字段外,所有内容均为UITextfield,“说明”字段为UITextView

所以我想要的是当我导航到这个视图时,

  1. 如果我进行了一些更改或修改,则只启用保存按钮
  2. 如果我编辑了一些字段'值并尝试返回上一屏幕而不保存,应弹出警告。
  3. enter image description here

    如何实现呢?感谢。

2 个答案:

答案 0 :(得分:0)

使用验证器函数通过检查文本计数的任何字段是否为>来验证整个表单。 0.为了让您的生活更轻松,有一些验证器可供您使用,例如RAFieldValidator。在github上寻找更多信息。检查这些https://github.com/search?utf8=%E2%9C%93&q=form+validator+ios&type=

开始触发此功能,

对于文字字段:请查看UITextField text change event

对于TextView:您需要设置UITextView委托并在其中实现textViewDidChange:Method。

答案 1 :(得分:0)

查看以下示例,我希望它能为您提供帮助。

#import "SampleViewController.h"

@interface SampleViewController ()

@end

@implementation SampleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _textview.delegate = self;

    _secondButton.hidden = YES;

    self.textview.layer.borderWidth = 2.0f;
    self.textview.layer.borderColor = [[UIColor grayColor] CGColor];

    self.navigationItem.hidesBackButton = YES;
    UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"< back" style:UIBarButtonItemStylePlain  target:self action:@selector(back:)];
    self.navigationItem.leftBarButtonItem = newBackButton;


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    _secondButton.hidden = NO;
}

- (void) back:(UIBarButtonItem *)sender {
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Attention"
                                                                   message:@"Are you sure want to discard this message."
                                                            preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                                                              self.textview.text = nil;
                                                              [self.navigationController popViewControllerAnimated:YES];
                                                          }];

    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];
    NSLog(@"sandip");
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)secondButton:(id)sender {
}
@end
相关问题