私は3つのView Controller A、B、Cを持っています.. AからCにデータを渡したいです... Cでプロパティを作成し、それをAで使用する必要があることを知っています.
しかし、理由はわかりません.. A に値を設定した後も、ビュー C に null 値が表示されます..
与えられた提案に従って、私は新しいプロジェクトと新しいコードを作成しました...私が達成したいのはビューAから簡単ですビューCで値を渡したい..しかし、現在はnullを与えています..
私のビュー1.hファイル...
@interface ViewController : UIViewController
- (IBAction)ActionButton:(id)sender;
@end
1.m ファイルを表示します。
#import "ViewController.h"
#import "ViewController3.h"
#import "ViewController2.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)ActionButton:(id)sender {
ViewController3 *v3 = [[ViewController3 alloc]init];
v3.string = @"String";
// [self.view addSubview:v3.view];
ViewController2 *v2 = [[ViewController2 alloc]initWithNibName:@"ViewController2" bundle:nil ];
[self.view addSubview:v2.view];
}
@end
2.h ファイルを表示...
@interface ViewController2 : UIViewController
- (IBAction)ActionButton:(id)sender;
@property (nonatomic,retain) NSString *string1;
@end
2.m ファイルを表示
#import "ViewController2.h"
#import "ViewController3.h"
@interface ViewController2 ()
@end
@implementation ViewController2
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)ActionButton:(id)sender {
ViewController3 *v3 = [[ViewController3 alloc]initWithNibName:@"ViewController3" bundle:nil ];
[self.view addSubview:v3.view];
}
@end
3.h ファイルを表示します。
@interface ViewController3 : UIViewController
@property(nonatomic,retain) NSString *string;
@end
3.m ファイルを表示...
#import "ViewController3.h"
@interface ViewController3 ()
@end
@implementation ViewController3
@synthesize string;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(@"%@",string);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
参考までに、ビュー 1 でコメント付きのコードを削除すると、ビュー 3 で値が取得されますが、これにより、望ましくないシーケンスが変更されます...