-1

I know how to pass the data from one view controller to another view controller ,now i want to know how to pass a textfield value from view controller to NSObject class and how to store the recieved in nstring .Please help me to do this , Can anyone give a example for this ,

  • (I assume you mean `NSObject` subclass.) What does the class look like and how is an object of the class related to the controller? – Phillip Mills May 22 '16 at 17:52
  • @Phillip Mills ya its a NSObject subclass, In a View Controller there is a textfield i just pass that into nsobject and then i want to perform some process over there , – user6183984 May 22 '16 at 17:57
  • 1
    I don't understand what's confusing you. Create a `NSString` property in the .h file for the subclass and set the property from the view controller. The controller obviously needs a reference to the object but how to do that depends on answering my question: "*how is an object of the class related to the controller*". – Phillip Mills May 22 '16 at 18:19

1 Answers1

1

I think what you're asking is how to store data in a model object for use by your view controller. If this is not your meaning, then please forgive me.

You are right that a model object should inherit from NSObject. Optionally, you could also extend another model object to add property values. Model objects are a great way to separate the view objects from your data.

Let's say you have a CustomerViewController with some customer fields. You need to populate those customer fields, and potentially perform some processing on that data. The model object supports these relationships, by allowing you to separate your views from any processing logic related to your data and business rules.

Using the relationships below as a guide, you should be on your way to building effective view controllers, that separate your views from your data!

CustomModel Interface

@interface CustomerModel : NSObject

@property (strong, nonatomic) NSString *firstName, *lastName;
@property (strong, nonatomic) NSString *phoneNumber;

- (BOOL) isValidPhoneNumber:(NSString *)phoneNumber;

@end

CustomerModel Implementation

#import "CustomerModel.h"

@implementation CustomerModel

- (BOOL) isValidPhoneNumber:(NSString *)phoneNumber
{
    //Check that phone number can be parsed and is valid
}

@end

CustomerViewController Implementation

#import "CustomerViewController.h"
#import "CustomerModel.h"

@interface CustomerViewController () <UITextFieldDelegate>

@property (strong, nonatomic) CustomerModel *customerModel;
...
@property (weak, nonatomic) IBOutlet UITextField *firstNameField
...

@end


@implementation CustomerViewController

- (void) viewDidLoad {
    //Optionally instantiate the customer model with stored data,
    //  to pre-populate the view controller.

    self.customerModel = [CustomerModel new];

    self.firstNameField.delegate = self;
}

- (void) textFieldDidEndEditing:(UITextField *)textField {
    //Validate the phone number
    NSString *phoneNumber = textField.text;
    if ([self.customerModel isValidPhoneNumber:phoneNumber]) {
        self.customerModel.phoneNumber = phoneNumber;
    } else {
        //Alert the user that the data is invalid
    }

}

- (BOOL) textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

@end

Possible improvements

  • It might be a little annoying to the User, to see alerts while filling out information. So, it might be better to defer the validation to when a save button is pressed.
  • Model objects can be populated from a data store, to be used to pre-populate a form.
Sheamus
  • 6,506
  • 3
  • 35
  • 61