Pass a NSString from Child to Parent for ModalViewController
-
29-09-2019 - |
Question
My parent ViewController class contains a UISearchBar. I have a UIButton that will push on a new ViewController via the code below
AddViewController *addViewController = [[AddViewController alloc] initWithNibName:@"AddView" bundle:nil];
[self presentModalViewController:addViewController animated:YES];
[addViewController release];
In the AddViewController the User is presented with a TableView of animal names. On the
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
I extract the the animal name and store it in an NSString. I then dismiss the AddViewController by
[self dismissModalViewControllerAnimated:YES];
My Question is How do I pass the NSString animal name to the Parent ViewController of AddViewController? I'm attempting to place the animal name in the UISearchBar
Solution
You would need to create a Delegate.
http://iosdevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html
These allow you to communicate with the parent view controller, and in your case, pass a variable.
OTHER TIPS
You could create a property in the parent view controller which you can then update from the child by calling
self.parentViewController.searchBarText = animalNameString;
When the child is dismissed, set the UISearchBar text in the parent's viewWillAppear method