2013-11-04 4 views
1

내가 의 ViewController 내가 다른 사람의 viewControllers을 만들 것이라고 처음보기에 내 객체를 만들어 내 데이터 데이터 이 포함되어 내 CLASSE이 내가 읽기 및 쓰기 할 사이에 개체를 전달 내 ViewController에서 생성 된 "man1"객체의 데이터. 어떻게하면됩니까? 대단히 감사합니다.IOS는 2 개 이상의 뷰 컨트롤러

Data.H

#import <Foundation/Foundation.h> 

@interface Data : NSObject 
{ 
    NSString *name; 
    int age; 
    NSString *city; 
} 
- (id)initWithName:(NSString *)aName ; 

- (NSString*) name; 
- (int) age; 
- (NSString*) city; 

//- (void) setPrenom:(NSString*) prenom; 
- (void) setName:(NSString*) newName; 
- (void) setAge:(int) newAge; 
- (void) setCity:(NSString*) newCity; 

@end 

Data.m

#import "Data.h" 

@implementation Data 


- (id)initWithName:(NSString *)aName 
{ 
    if ((self = [super init])) 

    { 
    self.name = aName; 

} 
    return self; 

} 


//getter 
- (NSString*) name 
{ 
    return name; 
} 

- (int) age{ 
    return age; 

} 

- (NSString*) city{ 
    return city; 
} 


//setter 
- (void) setName:(NSString*)newName 
{ 
    name = newName; 
} 
- (void) setAge:(int) newAge 
{ 
    age = newAge; 
} 
- (void) setCity:(NSString *)newCity 
{ 
    city = newCity; 
} 



@end 

ViewController.h

#import <UIKit/UIKit.h> 
#import "Data.h" 

@interface ViewController : UIViewController 
{ 
    int testint; 

} 


@property (readwrite) Data *man1; 
@property (weak, nonatomic) IBOutlet UILabel *labelAff; 


@end 

ViewController.m

:이 지금까지 내 코드입니다

16,

#import "ViewController.h" 
#import "Data.h" 

@interface ViewController() 

@end 

@implementation ViewController 
@synthesize man1 = _man1; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 


    NSString * name1 = @"Bob"; 
    _man1 = [[Data alloc]initWithName:name1 ]; 
    NSLog(@" %@ ", _man1.name); 

    [_man1 setAge:29]; 
    NSLog(@" %d ", _man1.age); 


    [_man1 setCity:@"Tapei"]; 
    _labelAff.text = [_man1 city]; 

} 



- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

답변

0

당신의 접근 방식은 Data의 뷰가로드 될 때마다 새로운 인스턴스를 할당하기 때문에, 작동하지 않을, 각 뷰 컨트롤러는 자신의 Data 개체를 얻을 수 있기 때문이다.

이 문제를 해결하는 방법 중 하나는 making your Data class a singleton입니다.

Data.h

@interface Data : NSObject 
{ 
    NSString *name; 
    int age; 
    NSString *city; 
} 
- (id)initWithName:(NSString *)aName ; 

- (NSString*) name; 
- (int) age; 
- (NSString*) city; 

- (void) setName:(NSString*) newName; 
- (void) setAge:(int) newAge; 
- (void) setCity:(NSString*) newCity; 
+(Data*)instance; 
@end 

Data.m

@implementation Data 

-(id)initWithName:(NSString *)aName { 
    if(self=[super init]) { 
     ... 
    } 
    return self; 
} 

+(Data*)instance { 
    static dispatch_once_t once; 
    static Data *sharedInstance; 
    dispatch_once(&once, ^{ 
     sharedInstance = [[self alloc] initWithName: ...]; 
    }); 
    return sharedInstance; 
} 
@end 
+0

가 대단히 감사합니다 : 귀하보기 컨트롤러는 정보가 뷰 컨트롤러간에 공유되는 것을 보장 Data의 단일 인스턴스를 액세스하는 것 ,이 문제를 먼저 해결할 것입니다 –

+0

@MaxPuis 시도해 보셨습니까? 작동 했나요? – dasblinkenlight

+0

내 객체에 싱글 톤을 사용하지만 여전히 View 컨트롤러간에 데이터를 공유하는 방법을 모른다. –

관련 문제