I have an array which I’m using to populate 2 cells with text for now. In a different view I have a textfield, a textview etc..
How can I move the data input by user in the textfield into an array in a different controller.
here’s what I have:
- (void)viewDidLoad
{
tabledata = [[NSArray alloc] initWithObjects:@"Franklin", @"delossantos", nil];
[super viewDidLoad];
}
Here is the view controller of the textfields:
NewEntryController.m
#import "NewEntryViewController.h"
@implementation NewEntryViewController
@synthesize titleTextfield;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
#pragma mark - View lifecycle
- (void)viewDidUnload
{
[self setTitleTextfield:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
@end
I guess there are two options:
NSObjectclass and make it a shared data model for both of your view controllers. Write to it when user inputs something.Also, you declare an immutable array, you won’t be able to change it. You will have to either recreate or declare it as mutable.
More on the first option
In order to create a shared model, you have to create a separate class.
Add some property to store data there, e.g.
@property (strong, nonatomic) NSMutableArray *userStrings;Add
@synthesizeand initialise it in theinitmethod of your new class.Create an instance of it and assign it to some property in both view controllers, say, to:
@property (strong, nonatomic) NSObject *dataModel;After you assign it, you will be able to access it from inside your view controllers. There, when your user inputs something, you will need to update contents of your
dataModelby adding, changing or removing elements in the model’s array. It will be done in some similar way:This way you can have a shared datasource.
Possible easier solution
It may be the case that you don’t need the model to be shared simultaneously between two controllers, and maybe you open second one only after user has finished inputing and you create it from your first controller.
In that case you don’t have to create and share the model, you can just create an array based on user input and pass it in your second controller. You would create that second controller and set it’s property
tableData. When your next controller appears it’d just read from that property and show the data.If you need more info on such approaches, I’d suggest you watch some video tutorials on using MVC approach in Objective-C. For example, Stanford’s course on iOS Programming is awesome. It is available for free in the iTunes U.