1

I have 3 view controllers A,B,C .. I want to pass data from A to C... I know i have to create a property in C and use it in A.

But I dont know why.. after setting the value in A i still get a null value in the view C..

As per given suggestions i have created a new project and a new code... What i want to achieve is simple from view A i want a value to be passed in view C..but currently it is giving null..

My view 1.h file...

@interface ViewController : UIViewController
- (IBAction)ActionButton:(id)sender;

@end

view 1.m file..

#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

View 2.h file...

@interface ViewController2 : UIViewController
- (IBAction)ActionButton:(id)sender;
@property (nonatomic,retain) NSString *string1;
@end

View 2.m file

#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

View 3.h file..

@interface ViewController3 : UIViewController

@property(nonatomic,retain) NSString *string;

@end

And View 3.m file...

#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

FYI if i remove the commented code in view1 i get the value in view 3, but this changes my sequence which i don't want...

Adolf Dsilva
  • 13,092
  • 8
  • 34
  • 45

4 Answers4

3

Creating a new answer because the modified question adds more clarity to the issue.

In view1.m you do the following

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

which creates a new instance of ViewController3 and sets the property value.

Then in view2.m you do

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

which creates another new instance of ViewController3 (this time with the nib layout) but you don't set the property value.

What you've done is created 2 distinct instances of the same object but only set the property value of one of them - the one you are not displaying.

Either set the property value of ViewController3 in the ActionButton method of ViewController2 or you need to pass your instance of ViewController3 to ViewController2 and then add the subview from that one.

  • That's what i thought..but not a very proper way .... Does this also implies that there is no way of passing data from view1 to view3??? – Adolf Dsilva Jul 08 '13 at 14:42
  • 1
    A lot of it depends on the data source and the architecture of the application. For something simple you would just pass data from v1 to v2 and then on to v3. If v1 is the root view controller on a nav stack you could set the value in a property on v1 and then retrieve the instance of it from v3 by using (ViewController1 *)[self.navigationController.viewControllers objectAtIndex:0] and get the value from the property that way. – Brandon Campbell Jul 08 '13 at 14:58
0

Try printing it like this:

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

If you didnt synthesize it, it will be synthesized by default like this:

@synthesize WithdrawType = _WithdrawType

So you need to use the self. to access the setter

If not, are you usre you are using the same controller?

Try also:

Denomination *deno= [[Denomination alloc] initWithNibName:@"Denomination" bundle:nil]; 
Antonio MG
  • 20,382
  • 3
  • 43
  • 62
0

Sounds to me like an order of execution problem. If I had to guess the -(id)viewDidLoad method in controller C is executing before deno.WithdrawType = @"ATM"; in controller A.

Try setting some breakpoints or moving the NSLog statements from viewDidLoad to viewWillAppear to see if that solves it. For a solution you could probably create an -(id)initWithWithdrawType:(NSString *)withdrawType method to initialize your view controller and ensure you are controlling the flow of the data.

Be wary that viewDidLoad might run when you do self = [super init] in your new method so set the property value first.

-2

You need to

@property (retain,nonatomic) NSString *WithdrawType;

Synthesize that property in .m class.

Jitendra
  • 5,055
  • 2
  • 22
  • 42