Reputation: 63
I want to keep my project better organized and instead of overloading my Appdelegate
with data that I want to transfer from viewcontroller
to viewcontroller
I want to create a model class to help keep things more organized. However, when using models I am having a hard time transferring data between controllers. Can you show me the error of my ways when trying to transfer this nsstring data from ViewController1
to ViewController2
? P.S. I made this example up , because my real project is a little bit more messy so I apologize in advance for any inconsistencies. The following results in NSLog
reporting null in
ViewController2.m
ViewController1.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
ViewController2 *viewController2 =[[ViewController2 alloc]init];
ViewControllerModel *vcm = [self.array objectAtIndex:indexPath.row];
NSLog(@"%@",vcm.string) // this will output a number
[self.navigationController pushViewController:viewController2 animated:YES];
}
// this delegate fetches an array of json data
-(void)fetchedResults:(NSMutableArray*)arrayList{
self.array = arrayList;
}
ViewController2.m
- (void)viewDidLoad
{
ViewControllerModel *vcm = = [[ViewController alloc] init];
[super viewDidLoad];
NSLog(@"%@",vcm.string); // this will output null.
}
ViewControllerModel .h #import
@interface ViewControllerModel : NSObject
@property (nonatomic, strong) NSString *string;
@end
ViewControllerModel.m
#import "ViewControllerModel.h"
@implementation ViewControllerModel
@synthesize string;
@end
MyHandler.m
//this is where vcm.string in ViewController1.m will get all the numbers not sure if this is needed but just in case .
for (NSDictionary *dict in responseArray)
{
ViewControllerModel *vcm = [[ViewControllerModel alloc] init];
vcm.string = ([dict valueForKey:@"string"] == [NSNull null]) ? @"" : [NSString stringWithFormat:@"%@",[dict valueForKey:@"string"]];
Upvotes: 1
Views: 68
Reputation: 1697
Make the following changes:
ViewController1.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
ViewController2 *viewController2 =[[ViewController2 alloc]init];
ViewControllerModel *vcm = [ViewControllerModel alloc] init];
vcm.string = @"My String";
viewController2.vcm = vcm;
NSLog(@"%@",vcm.string) // this will output a number
[self.navigationController pushViewController:viewController2 animated:YES];
}
// this delegate fetches an array of json data
- (void)fetchedResults:(NSMutableArray*)arrayList{
self.array = arrayList;
}
ViewController2.h
@interface ViewController2 : NSObject
@property (nonatomic, strong) ViewControllerModel *vcm;
@end
ViewController2.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"%@", self.vcm.string);
}
I would also recommend the following improvements.
You don't really need a model object at this point. You could just add the string as an NSString property to ViewController2 instead of the ViewControllerModel object:
@property (nonatomic, copy) NSString *string;
I would recommend naming your properties, model object and view controllers to something more descriptive. Even if it's a sample it will be hard to understand it if you don't.
When you create an NSString property (or any other class that has a mutable equivalent) I'd recommend using 'copy' instead of 'strong'. This will make an immutable copy of the string if an NSMutableString is assigned to the property which is considered a safer approach.
Upvotes: 1