2010-03-09 54 views
2

我知道這個問題已被問了好幾次,我確實已經閱讀了關於這個主題的現有文章,但我仍然需要幫助。UIViewController parentViewController訪問屬性

我有2 UIViewControllers - 父母和孩子。我使用presentModalViewController如下顯示子UIViewController

ChildController *child = 
[[ChildController alloc] initWithNibName:@"ChildView" bundle:nil]; 
[self presentModalViewController:child animated:YES]; 
[child release]; 

子視圖具有UIPickerView。當用戶從UIPickerView中選擇一個項目並點擊完成時,我必須關閉模態視圖並在父視圖中的UITextField上顯示所選項目。

在孩子的按鈕點擊delegate,我做了以下內容:

ParentController *parent = 
(ParentController *)[self.navigationController parentViewController]; 
[parent.myTextField setText:selectedText]; 
[self dismissModalViewControllerAnimated:YES]; 

一切正常,沒有錯誤。但我不知道如何加載父視圖,以便它顯示更新的UITextField

我試圖

[parent reloadInputViews]; 

沒有按」工作。請幫忙。

回答

13

代表團是要走的路。我知道一些人可能正在尋找一個更簡單的解決方案,但相信我,我已經嘗試了其他人,沒有什麼比代表團更好。所以,任何人都有同樣的問題,請閱讀委派並逐步遵循。

在你的subviewcontroller.h - 聲明一個協議並在其中聲明委託方法。

@protocol myDelegate 
-(void)clickedButton:(subviewcontroller *)subController; 
@end 

在你subviewcontroller.h,@interface中:

id<myDelegate> delegate; 
@property (nonatomic, assign) id<myDelegate> delegate;  
NSString *data; 
-(NSString *)getData; 

在你subviewcontroller.m,綜合myDelegate。添加以下代碼到你想通知你parentviewcontroller該子視圖做是做什麼它應該做的事:

[delegate clickedButton:self]; 

,然後處理的getData返回你要發送到您的parentviewcontroller任何數據

在你parentviewcontroller.h,進口subviewcontroller.h和使用它的委託

#import "subviewcontroller.h" 
@interface parentviewcontroller : VUIViewController <myDelegate> 
{} 

在你parentviewcontroller。m,執行代理方法

​​

不要忘記內存管理!

+0

+1這是一個很好的答案。你救我的頭。謝謝一堆。 – 2012-12-11 09:12:06

+0

這就是Effin史詩答案哥們。謝謝!。 :-) – CalZone 2013-07-31 10:12:29

0

如果在模態視圖顯示期間出現低內存警告,則父視圖將被卸載。然後parent.myTextField在視圖重新加載之前不再引用正確的文本字段。您可以通過調用parent.view;

來強制重新加載視圖。但是,更好的想法可能是讓父視圖具有可由子視圖設置的String屬性。然後,當父視圖重新出現時,將該數據放入文本字​​段中,例如viewWillAppear:。當父視圖最初顯示時,您希望將該值設置爲某個默認值。

+0

viewWillAppear在子模態被解除時不會觸發。 '完成'按鈕位於子視圖中,我可以看到選擇器視圖的選定文本,然後關閉模式。父母就在那裏,不必重新出現。我猜這就是爲什麼它不會觸發viewWillAppear。 – Dave 2010-03-09 23:19:58

+0

如果你正在做的一切都正確,按照它應該使用的方式使用框架,應該調用viewWillAppear。你確定你在正確的課程中壓倒了正確的方法嗎? – 2010-03-10 02:27:22

0

- (void)viewWillAppear:(BOOL)animated也不會爲我調用,當它是模態視圖控制器時。不知道爲什麼。在此應用程序的任何位置都沒有被錯誤覆蓋,並且我正在處理的另外兩個應用程序也出現同樣的問題。我真的不認爲它有效。

我已經使用委託方法,但我認爲以下方法也很好。

我解決此加入私人類別的UIViewController,像這樣:

.h文件中:

@interface UIViewController(Extras) 
// returns true if this view was presented via presentModalViewController:animated:, false otherwise. 
@property(readonly) BOOL isModal; 
// Just like the regular dismissModalViewController, but actually calls viewWillAppear: on the parent, which hasn't been working for me, ever, for modal dialogs. 
- (void)dismissModal: (BOOL) animated; 
@end 

和.m文件:

@implementation UIView(Extras) 
-(BOOL) isModal 
{ 
    return self == self.parentViewController.modalViewController; 
} 

- (void)dismissModal: (BOOL) animated 
{ 
    [self.parentViewController viewWillAppear: animated]; 
    [self dismissModalViewControllerAnimated: animated]; 
} 
@end 

,我現在可以當我想關閉對話框時打電話給我:

// If presented as a modal view, dismiss yourself. 
if(self.isModal) 
    [self dismissModal: YES]; 

現在viewWillAppear被正確調用。

是的,我正在捐贈'isModal'屬性獎金,以便模態視圖可以告訴它如何呈現,並適當地駁回自己。