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 😉
Instead of
Create your own initializer, and pass in a Person object. i.e.
DataViewController.h
DataViewController.m
HomeViewController.m