好的,所以我有一个包含以下文件的小程序;
- HomeViewController(.h .m .xib)
- DataViewController(.h .m .xib)
- AppDelegate(.h .m)
- 人(.h .m)[OBJECT]
+支持文件等......
假设我通过HomeViewController中的文本框和其他输入获取用户输入,并将它们设置为Person实例(年龄,等级等)的属性,如何从DataViewController访问它们(相同的实例)显示它们?
以下是来自HomeViewController.h和.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];
}
这里有一点Person.h和.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
注:
- 使用导航控制器
- iOS 6
- xCode 4.5.1
+如果您需要更多信息,请告诉我们! :)
附:我对这些东西有点新意,所以这可能是一个愚蠢的问题;)
答案 0 :(得分:2)
而不是
DataViewController *vc = [[DataViewController alloc]init];
创建自己的初始值设定项,并传入Person对象。即。
<强> 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];
}
答案 1 :(得分:0)
我可以想到几种方法来做到这一点。
1-在HomeViewController中创建一个包含Person对象的Container或List 然后,您可以在DataViewController中调用此列表,并使用get()方法获取所需的人
2-如果你的应用程序中只需要一个人(如果你在应用程序中需要多个人对象,这将无法工作)你可以让人成为单身人士,然后每次你尝试在你编程时创建一个Person对象将首先检查此对象的实例是否已经存在,如果一个实例不存在,它将返回一个新实例,但如果一个人对象已经存在,它将只返回现有人。
Here是singleton patern的解释和示例代码