0

好的,所以我有一个包含以下文件的小程序;
- HomeViewController (.h .m .xib)
- DataViewController (.h .m .xib)
- AppDelegate (.h .m)
- Person (.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
+ 如果您需要更多信息,请告诉我!:)

PS 我对这些东西有点陌生,所以这可能是一个愚蠢的问题;)

4

2 回答 2

2

代替

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

创建您自己的初始化程序,并传入一个 Person 对象。IE

数据视图控制器.h

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

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

数据视图控制器.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;
}

主视图控制器.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];
}
于 2012-10-27T17:39:19.347 回答
0

我可以想到几种方法来做到这一点。

1- 在 HomeViewController 中创建一个包含 Person 对象的容器或某种类型的列表然后您可以在 DataViewController 中调用此列表并使用 get() 方法来获取您想要的人

2-如果您的应用程序中只需要一个 Person (如果您在应用程序中需要多个 person 对象,这将不起作用)您可以将 person 设为单例,然后每次尝试在程序中创建 Person 对象时,它都会首先检查如果这个对象的一个​​实例已经存在,如果一个不存在,它将返回一个新实例,但如果一个人对象已经存在,它只会返回现有的人。

是单例模式的解释和示例代码

于 2012-10-27T17:58:58.430 回答