将数据从一个视图控制器传递给另一个给出null

时间:2013-07-08 13:03:17

标签: ios objective-c uiviewcontroller

我有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

如果我删除了view1中的注释代码,我会在视图3中获取值,但这会改变我不想要的序列...

4 个答案:

答案 0 :(得分:3)

创建新答案,因为修改后的问题可以更清晰地解决问题。

在view1.m中,您可以执行以下操作

ViewController3 *v3 = [[ViewController3 alloc]init];
v3.string = @"String";

创建ViewController3的新实例并设置属性值。

然后在view2.m中你做

- (IBAction)ActionButton:(id)sender {
    ViewController3 *v3 = [[ViewController3 alloc]initWithNibName:@"ViewController3" bundle:nil ];
    [self.view addSubview:v3.view];
}

创建ViewController3的另一个新实例(这次是使用nib布局),但是你没有设置属性值。

您所做的是创建同一对象的2个不同实例,但只设置其中一个的属性值 - 您未显示的属性值。

在ViewController2的ActionButton方法中设置ViewController3的属性值,或者需要将ViewController3的实例传递给ViewController2,然后添加该视图中的子视图。

答案 1 :(得分:0)

尝试按照以下方式打印:

NSLog(@"Type %@",self.WithdrawType);

如果你没有合成它,它将默认合成,如下所示:

@synthesize WithdrawType = _WithdrawType

所以你需要使用自我。访问setter

如果没有,您是否使用相同的控制器?

另请尝试:

Denomination *deno= [[Denomination alloc] initWithNibName:@"Denomination" bundle:nil]; 

答案 2 :(得分:0)

听起来像是执行问题的顺序。如果我不得不猜测控制器C中的 - (id)viewDidLoad方法是在deno.WithdrawType = @“ATM”之前执行的;在控制器A中。

尝试设置一些断点或将NSLog语句从viewDidLoad移动到viewWillAppear以查看是否解决了这个问题。对于解决方案,您可以创建 - (id)initWithWithdrawType:(NSString *)withdrawType方法来初始化视图控制器并确保控制数据流。

警惕viewDidLoad可能会在你的新方法中执行self = [super init]时运行,所以首先设置属性值。

答案 3 :(得分:-2)

你需要

@property (retain,nonatomic) NSString *WithdrawType;

在.m类中合成该属性。

相关问题