2012-10-27 92 views
0

好吧,我有一個小程序,它具有以下文件;
- HomeViewController(.H .M的.xib)
- DataViewController(.H .M的.xib)
- 的AppDelegate(.H .M)
- 人(.H .M)[對象]
+支持文件等...
在視圖控制器之間共享對象實例

假設我通過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
+如果您需要任何更多的信息,讓我知道! :)

P.S.我有點新的這個東西所以這可能是一個愚蠢的問題;)

回答

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]; 
} 
+0

我試圖左右逢源,但對你HomeViewController.h改了行說有語法錯誤:「DataViewController」不可見@interface聲明的選擇「initWithPerson:」 – WillB

+0

錯字:HomeViewController.m ...對不起:P – WillB

+0

我很抱歉@WillB,我混了文件名。看我的編輯。 – WDUK

0

我可以想到一些方法來做到這一點。

1-創建某種在你HomeViewController將包含人的容器或對象列表 然後,您可以撥打DataViewController此列表,使用get()方法來得到你想要的

2-人如果你只需要一個人在你的應用程序(如果您的應用程序需要多個人的對象,這將無法正常工作),你可以把人一個單,然後每一次嘗試在你的程序會首先檢查是否有創建一個Person對象此對象的實例已經存在,如果不存在,將返回一個新的實例,但在人的對象已存在,只是返回你現有的人的情況下。

Here是單身百通的解釋和示例代碼

+0

所以,如果我想使用Singleton模式,我會怎麼做呢?這些例子是在java中,我很難將其轉換爲我的應用程序。 – WillB

相關問題