我有 3 个视图控制器 A、B、C .. 我想将数据从 A 传递到 C... 我知道我必须在 C 中创建一个属性并在 A 中使用它。
但我不知道为什么.. 在 A 中设置值后,我仍然在视图 C 中得到一个空值..
根据给定的建议,我创建了一个新项目和一个新代码......我想要实现的很简单,从视图 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 中得到值,但这会改变我不想要的序列...