5

Possible Duplicate:
dismissModalViewController AND pass data back

I'm new to ios development and stuck on this problem:

I'm using storyboarding and have a navigation controller, vcA, with a TableView in it which shows some data from a MutableArray (which is initialized in viewdidload of the same class). After selecting any cell, a second view controller, vcB, is shown with a TextField in it and a button called "Add to list".

What I want is that when I enter some text in the TextField and press the "Add to list" button the text should be added to the array of previous view (which gets shown in the TableView) and when i tap the "Back" button on vcB's navigation bar, vcA should show the updated TableView with the new entry in it (on the top of list). Basically I want to add the text from vcB's TextField to the array of vcA and show the new array after clicking the BACK button.

I have searched a lot about this issue and seem to find that delegate and protocols is the way to achieve the desired result but I'm having trouble understanding delegation.

Community
  • 1
  • 1
Hyder
  • 1,163
  • 2
  • 13
  • 37
  • 2
    [What Have you Tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – Foggzie Dec 31 '12 at 21:32
  • 2
    This has been asked and answered too many times to count. http://stackoverflow.com/a/6204427/1271826 – Rob Dec 31 '12 at 21:53

1 Answers1

13

I have the second view controller presenting itself as modal in this example:

In the second view controllers h file:

@protocol SecondViewControllerDelegate <NSObject>
- (void)addItemViewController:(id)controller didFinishEnteringItem:(NSString *)item;
@end

@interface SecondPageViewController : UIViewController <UITextViewDelegate>
{
     NSString *previouslyTypedInformation;
}

@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (nonatomic) NSString *previouslyTypedInformation;
@property (nonatomic, weak) id <SecondViewControllerDelegate> delegate;

In the second view controllers m file make sure to synthesize properties and add then add this:

- (IBAction)done:(id)sender
{
     NSString *itemToPassBack = self.textView.text;
     NSLog(@"returning: %@",itemToPassBack);
     [self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack];
     //dismiss modal view controller here
}

Then in the first view controllers h file set it as delegate:

 @interface FirstPageViewController: UIViewController <SecondViewControllerDelegate>
 @property (nonatomic) NSString *returnedItem;

Then in the first view controller's m file synthesize and add the method:

- (void)addItemViewController:(SecondPageViewController *)controller didFinishEnteringItem:    (NSString *)item
{
    //using delegate method, get data back from second page view controller and set it to property declared in here
    NSLog(@"This was returned from secondPageViewController: %@",item);
    self.returnedItem=item;

    //add item to array here and call reload
}

Now you have the text of what was returned! You can add the string to your array in the first view controller's viewDidLoad and call

[self.tableView reloadData]; 

and it should work.

gg13
  • 574
  • 6
  • 19
  • 1
    Just an addition, where you initialize your second view controller in your first one, do: secondView.myDelegate = self; – Hyder Jan 01 '13 at 08:40
  • gg13, thank you! The question for your answer should be called "How to easily create delegates". I was felt confused about it, and now not even searching for it, I understand them. – Mário Carvalho Apr 20 '13 at 22:38
  • itemToPassBack is working but when i try it to pass to FirstPageViewController ,it does not moving to FirstPageViewController – Purushothaman Nov 21 '16 at 10:36