1

Here is my View,enter image description here

In this view, except Description field all are UITextfield and Description field is UITextView.

So what I want is that when I navigate to this view,

  1. If I made some changes or modification then only save button will be enabled
  2. And If I edited some fields' values and tried to go back to previous screen without saving it, following alert should pop up.

enter image description here

How to achieve it ? Thanks.

Community
  • 1
  • 1

2 Answers2

0

Use a validator functions to validate the whole form by checking if any of the fields text count is > 0. To make your life easier, there are a few validators out there, which you can use such as, RAFieldValidator. Look out for more on github. Check these out https://github.com/search?utf8=%E2%9C%93&q=form+validator+ios&type=

Coming to triggering this function,

For Text field: check this out, UITextField text change event

For TextView: You need to set UITextView delegate and implement textViewDidChange: Method in it.

0

check following example, I hope it will help you.

#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
Nikita Patil
  • 674
  • 1
  • 7
  • 17