Pass a NSString from Child to Parent for ModalViewController

落花浮王杯 提交于 2019-12-25 04:47:09

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/4125226/pass-a-nsstring-from-child-to-parent-for-modalviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!