1

I have 2 view controllers MainDetailViewController (MD) and MainEditViewController (ME)

There is a textView in MD with some text already there when view loads. Then I call ME like this

MainEditViewController *editVC = [[MainEditViewController alloc] init];
editVC.theTextView.text = self.theTextView.text;
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:editVC];
[self.navigationController presentViewController:navCon animated:YES completion:nil];

I am passing the TextView's text value to ME like above and calling it with presentViewController method.

In ME I edit the text and click on save button which should update the text value in MD's textView

MainDetailViewController *mainDetailVC = [[MainDetailViewController alloc] init];
mainDetailVC.theTextView.text = self.theTextView.text;
[self dismissViewControllerAnimated:YES completion:nil];

This is not reflecting change in MD

What am I doing wrong?

April
  • 237
  • 4
  • 10

3 Answers3

0

Views are not loaded until you present the view, so you will need to create a property to hold that string value and in viewDidLoad, you should set the textfield with the string value.

chuthan20
  • 5,389
  • 1
  • 17
  • 21
  • I suspect my MD view doesn't reload when I dismiss ME view and that might be the reason its not updating. – April Nov 29 '13 at 14:27
  • How can I hold the value of child view controller in parent view controller? – April Nov 29 '13 at 14:30
0

The easiest way to do this is by using block. Add new block typedef to your MainDetailViewController.h file just after imports but before @interface:

typedef void (^ReturnBlock)(NSString *arg);

In MainEditViewController.h file between @interface and @end add public property:

@property(copy) ReturnBlock returnBlock;

This is your block which will be called when you dismiss your ME view controller. Next move to MainEditViewController.m file and replace code in save button from:

MainDetailViewController *mainDetailVC = [[MainDetailViewController alloc] init];
mainDetailVC.theTextView.text = self.theTextView.text;
[self dismissViewControllerAnimated:YES completion:nil];

to:

    if (self.returnBlock)
        self.returnBlock(self.theTextView.text);
    [self.navigationController popViewControllerAnimated:YES];

You should use popViewControllerAnimated: method instead of dismissViewControllerAnimated.

The last change left to do is in your MainDetailViewController.m file when you create editVC, add after:

editVC.theTextView.text = self.theTextView.text;

this:

editVC.returnBlock = ^(NSString *returnText) {
    NSLog(@"Returned text: %@", returnText);
    self.theTextView.text = returnText;
};

Happy coding.

Greg
  • 25,317
  • 6
  • 53
  • 62
  • Thanks Greg for your help. I will surely try your method as well. – April Nov 29 '13 at 16:45
  • I think solution with block is better because you keep your logic in one class. With delegate you have to keep it in multiple. – Greg Nov 30 '13 at 11:45
0

You need to use a protocol to retrieve data from B to A.

You can have a look here

and here.

Community
  • 1
  • 1
Jordan Montel
  • 8,227
  • 2
  • 35
  • 40