Search code examples
iosobjective-cquickdialog

Passing data from ViewControllerA to ViewControllerB(QuickDialog)


I'm new in iOS developer and QuickDialog.

I want to pass data from ViewControllerA to ViewControllerB.

It's work but in ViewControllerB i have a problem.

Example

In ViewControllerA.m

-(void) goToViewControllerB
{
    ViewControllerB *viewControllerB = [[ViewControllerB alloc]init];
    // send data 
    viewControllerB.ownerName = @"TestData";
    [self.navigationController pushViewController:viewControllerB animated:YES];
}

In ViewControllerB.h

@property (nonatomic, strong) NSString *ownerName;

In ViewControllerB.m

-(id) init
{
    NSLog(@"init");
    NSLog(@"ownerName : %@", _ownerName);

    self = [super initWithRoot:[self createViewElement]];

    return self;
}

-(void) viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"viewDidLoad");
    NSLog(@"ownerName : %@", _ownerName);
}

- (void) viewWillAppear:(BOOL)animated
{
     [super viewWillAppear:animated];

     NSLog(@"viewDidLoad");
     NSLog(@"ownerName : %@", _ownerName);
}

In Method init I create Style of thisPage using QuickDialog.

In Method createViewElement I want to show data of _ownerName but it's NULL.

This is Output of this example code.

>> init
>> ownerName : (null)
>> viewDidLoad
>> ownerName : TestData
>> viewAppear
>> ownerName : TestData

I Want to use this Data in Method createViewElement.

How to resolve it.

P.S. I tried to call method createViewElement in viewDidLoad or viewWillAppear but it's not work.


Solution

  • Your init method of ViewControllerB is called before you ever set the value of ownerName. See your first code snippet. You could alter your init to accept the string like this :

    - (id)initWithOwner:(NSString*)theOwnerName
    

    and then modify your ViewControllerA code to pass the owner name like this :

    viewControllerB = [[ViewControllerB alloc] initWithOwner:@"TestData"];