1

Possible Duplicate:
iOS: Communicating to parent view controller

Suppose I have 2 viewControllers named FirstViewController and SecondViewController

Now using navigation I go from FirstViewController to SecondViewController.

Now I have one array named resultArray on SecondViewController

and I want to get the value of resultArray on FirstViewController

then how can I get the value on FirstView Controller?

Any idea?

Community
  • 1
  • 1
user1954352
  • 1,577
  • 4
  • 14
  • 16
  • 1
    To pass data back from child to parent view controller you need to use **Protocols and Delegates**. See http://stackoverflow.com/questions/10600330/ios-communicating-to-parent-view-controller – DD_ Jan 31 '13 at 11:15

4 Answers4

1

The answer form @Nishant B (and the other subsequent answers) will work.

Since you're using a UINavigationController, and you want to access the controller one step back, you can also do this within your SecondViewController:

NSUInteger count = [[self navigationController].childViewControllers count];
FirstViewController* firstVC = [[self.navigationController childViewControllers] 
    objectAtIndex:count -2];
[firstVC setSomeValue:someValue];
Jasper Blues
  • 28,258
  • 22
  • 102
  • 185
  • NSUInteger count = [[self navigationController].childViewControllers count]; DatesTableViewController *datesTable = [[self.navigationController childViewControllers] objectAtIndex:count -2]; [datesTable setTempArray:resultArray]; [self.navigationController popViewControllerAnimated:YES]; – user1954352 Jan 31 '13 at 12:26
  • I have done like this but my tempArray gives me null value – user1954352 Jan 31 '13 at 12:26
  • There's another problem too, then. Try ome log statements or stepping through with a debugger. – Jasper Blues Jan 31 '13 at 22:23
0

What you can do here is create a Constructor in your first class for e.g.

-(void)initWithArrayOfSecondClass:(NSMutableArray *)arr_OfSecondClass;
{
    arr_FirstClassArray = [[NSMutableArray alloc]initWithArray:arr_OfSecondClass];

}

From your second class send this array as :

[objOfFirstClass initWithArrayOfSecondClass:resultArray];
Rushi
  • 4,553
  • 4
  • 33
  • 46
0

You can achieve it in following way:

in "FirstViewController" file:

1) Create array object for example: arrTest;

2) Create one method for example:

-(void)getArrayValue:(NSMutableArray *)pArr
{
     arrTest = pArr;
}

3) call method of "SecondViewController" before pushing it:

[objSecondViewController setParent:self];\

Now, in "SecondViewController" file:

.h file:

@class FirstViewController;

in .m file:

#import "FirstViewController.h"

And

1) Declare one variable in .h file:

id parent;

2) Declare and create one method ".h" and ".m" file:

-(void)setParent:(id)pID
{
    parent=pID;
}

3) When you come up with array then call method like:

[parent getArrayValue: <your_new_array>];

Hope, you got an idea of what you want.

Happy Coding!

Cheers!

Nishant B
  • 2,897
  • 1
  • 18
  • 25
0

There is also a method of using local notifications.

Say you want to send data from B to A.

Do the following in B when you press a button or so:

- (NSString *) saveAndExit
{
    [[NSNotificationCenter defaultCenter] postNotificationName: @"AnswerValue" object: answerView.text];
    [self.navigationController popViewControllerAnimated:YES];
    return @"abc";
}

And in A's viewDidLoad, add the following:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(incomingNotification:) name:@"AnswerValue" object:nil];

Also add the following method:

- (void) incomingNotification:(NSNotification *)notification{
NSString *result = [notification object];

}

The notification name has to be the same for both. In this case, the name is AnswerValue. This will send the value "abc" from B to A and gets stored in result.

Anil
  • 2,430
  • 3
  • 37
  • 55