2010-11-08 37 views
0

我的父級ViewController類包含一個UISearchBar。我有一個UIButton將通過代碼推新視圖控制器下面從ModalViewController傳遞一個NSString從Child到Parent

AddViewController *addViewController = [[AddViewController alloc] initWithNibName:@"AddView" bundle:nil]; 
[self presentModalViewController:addViewController animated:YES]; 
[addViewController release]; 

在AddViewController用戶呈現動物名稱的TableView中。在

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

我提取動物名稱並將其存儲在NSString中。然後,我通過

[self dismissModalViewControllerAnimated:YES]; 

我的問題駁回AddViewController是如何通過的NSString動物名的家長AddViewController的視圖控制器?我試圖在UISearchBar中放置動物名稱

回答

1

您需要創建一個委託。

http://iosdevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html

這允許你與父視圖控制器進行通信,並在你的情況,傳遞變量。

+0

我的的UISearchBar文本不會說「需要」 - 這是你*可以*的一種方式。 :-) – 2010-11-08 15:42:52

+0

沒有其他辦法可以乾淨地做到這一點。如果需要傳遞多個變量,使用委託將是最好的方法。 – 2010-11-08 15:46:15

+0

很棒的參考資料。這幫助我解決了我的問題 – aahrens 2010-11-08 17:35:45

0

您可以創建在父視圖控制器的屬性,你可以從孩子通過調用

self.parentViewController.searchBarText = animalNameString; 

當孩子被開除,然後更新,請在父母的viewWillAppear中方法