willbattel
willbattel

Reputation: 1120

Sharing an object instance between View Controllers

Ok so I have a small program that has the following files;
- HomeViewController (.h .m .xib)
- DataViewController (.h .m .xib)
- AppDelegate (.h .m)
- Person (.h .m) [OBJECT]
+ Supporting files, etc...

Say I am getting user input through text boxes and other inputs in HomeViewController and setting them as properties of an instance of Person (age, grade, etc... ), how do I access them (the same instance) from DataViewController to display them?

Here is a snippet of code from HomeViewController.h and .m
.h

@interface HomeViewController : UIViewController {

    IBOutlet UIButton *done;

    BOOL standardRes;
}

@property (nonatomic) int newAge;
@property (nonatomic) int newGrade;

-(IBAction)doneButtonPressed;

@end

.m

@implementation HomeViewController
@synthesize newAge, newGrade;

-(IBAction)doneButtonPressed{
    Person *user = [[Person alloc]init]; //Creating an instance of Person called user
    user.age = newAge; //Do I even need this if I use setters and getters in person.m?
    user.grade = newGrade; //Do I even need this if I use setters and getters in person.m?
    NSLog(@"Age: %i, Grade: %i", user.age, user.grade); //Testing with NSLog

    newAge = 175;
    [user setAge:(int)newAge]; //Calling setter method for property 'age' in Person.m

    DataViewController *vc = [[DataViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

And here is a little bit of Person.h and .m

@interface Person : NSObject {

}

@property (nonatomic, getter = age, setter = setAge:) int age;
@property (nonatomic) int grade;

@end


@implementation Person
@synthesize age, grade;

-(void)setAge: (int) newAge {
    age = newAge;
}

-(int)age {
    return age;
}

@end


NOTES:
- Using Navigation Controller
- iOS 6
- xCode 4.5.1
+ If you need any more info let me know! :)

P.S. I'm somewhat new to this stuff so this might be a dumb question ;)

Upvotes: 0

Views: 310

Answers (2)

WDUK
WDUK

Reputation: 19030

Instead of

DataViewController *vc = [[DataViewController alloc]init];

Create your own initializer, and pass in a Person object. i.e.

DataViewController.h

- (id)initWithPerson:(Person*)person;

// Need a way to store it, either through a property
@property (nonatomic, strong) Person* person;

DataViewController.m

// Or you can store it as an instance variable, via Class Extension
@interface DataViewController() {
    Person* m_person;
}
@end

// Either way you store it, do it here
-(id)initWithPerson:(Person*)person {
    self = [super init];
    if (self) {
        self.person = person;
        // OR
        m_person = person;
    }
    return self;
}

HomeViewController.m

-(IBAction)doneButtonPressed {
    Person *user = [[Person alloc]init]; //Creating an instance of Person called user
    user.age = newAge; //Do I even need this if I use setters and getters in person.m?
    user.grade = newGrade; //Do I even need this if I use setters and getters in person.m?
    NSLog(@"Age: %i, Grade: %i", user.age, user.grade); //Testing with NSLog

    newAge = 175;
    [user setAge:(int)newAge]; //Calling setter method for property 'age' in Person.m

    DataViewController *vc = [[DataViewController alloc]initWithPerson:user];
    [self.navigationController pushViewController:vc animated:YES];
}

Upvotes: 2

Mike Khan
Mike Khan

Reputation: 595

I can think of a couple of ways to do this.

1- Create a Container or List of some sort in your HomeViewController that will contain Person objects You can then call this list in DataViewController and use the get() method to get the person you want

2- If you only need one Person in your app (If you need multiple person objects in the app this will not work) you could make person a singleton and then every time you try to create a Person object in you program it will first check if an instance of this object already exists and if one does not exist it will return you a new instance but in the case where a person object already exists it will just return you the existing person.

Here is an explanation and sample code for the singleton patern

Upvotes: 0

Related Questions