1

I want to know how to pass values between views in popViewControllerAnimated .

Here is my scenario: I have a view which contains tableview on selecting the cell we go to another view where i need to enter value in textbox and i click a button to go back to the previous view where i need to display the textbox value in the table view cell.

How can i do this ?

This what i have done:

NewContact *nc = [[NewContact alloc] initWithNibName:@"NewContact" bundle:nil];
// ...
// Pass the selected object to the new view controller.
nc.name=[firstName text];
//[self.navigationController pushViewController:nc animated:YES];
[self.navigationController popViewControllerAnimated:YES];
[nc release];  
user746909
  • 105
  • 2
  • 4
  • 14

4 Answers4

4

In ViewControllerB, declare delegate and set action for popViewControllerAnimated:

@interface ViewControllerB : UIViewController {
    id delegate;
}

@property (nonatomic, retain) id delegate;

@synthesize delegate;

- (id) init ... {
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                              style:UIBarButtonItemStyleBordered
                                                                             target:self
                                                                             action:@selector(didBack:)] autorelease];
}

- (void) didBack:(id)sender {
    if ([delegate respondsToSelector:@selector(setProperty:)]) { 
        [delegate setProperty:property];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

In ViewControllerA, provide a function to set the local property and set delegate:

ViewControllerB controllerB = [[ViewControllerB alloc] init...];
[controllerB setDelegate:self];
[self.navigationController pushViewController:controllerB animated:YES];
[controllerB release];
morph85
  • 807
  • 10
  • 18
1

You need to store value in one of the global variable for example you can declare in appDelegate file. See this post for that

If you are using UITextField then you can store value in above variable from UITextField in below delegate method.

- (BOOL)textFieldShouldReturn:(UITextField *)textField;

Hope this help.

Community
  • 1
  • 1
Deeps
  • 4,399
  • 1
  • 26
  • 27
1

You could use NSNotificationCenter for this, passing an object along with the call.

Howto: Send and receive messages through NSNotificationCenter in Objective-C?

Community
  • 1
  • 1
Simeon
  • 5,519
  • 3
  • 29
  • 51
0

Based on your requirement in your comment,

Say you navigate from ViewControllerA instance to ViewControllerB instance and you wish to call ViewControllerA's methods, you can do it like this,

ViewControllerA * viewControllerA = (ViewControllerA *)self.parentViewController.
[viewController methodToCall];

To use this to address the requirement in the question, you can use a property.

viewControllerA.name = firstName.text; // Bits from your code snippet

However in a table view I would expect you to have a mutable array powering the data source. You can refer to it, assuming that it is an property.

[viewControllerA.dataSourceArray addObject:firstName.text];
Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
  • Making use of the delegation pattern, one could **avoid coupling** `ViewControllerA`and `ViewControllerB`, but it is a little more elaborate. – albertamg Jul 16 '11 at 15:29