私は自分のデータを含むクラスを持っています。ViewControllerという 最初のビューでオブジェクトを 作成しました。他のビューコントローラーを作成し、ViewController で作成されたオブジェクト「man1」でデータを読み書きしたいと考えています。どうやってやるの ?どうもありがとうございました。
これまでの私のコードは次のとおりです。
データ.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
データ.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
#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