2009-09-02 156 views
3

有誰知道如何從子視圖(子)視圖控制器更新屬性值? 我有一個int屬性,名爲statusid,在父視圖控制器中用gettor/settor定義。 [self.view addSubview:detailsVC.view];從子視圖控制器設置父視圖控制器類的屬性值?

在子子視圖中,我試圖調用[super statusid:updatedValue];將statusid更新爲新值,但這會產生錯誤。我怎樣才能更新父母的狀態ID?有人知道怎麼做嗎?

回答

0

在超級方法上調用方法調用方法的超類實現時,它調用超級視圖的超級視圖控制器的實現。

您可能需要在子視圖控制器中保留父對象的引用,並在父對象上調用setStatusId:方法,或者在兩者之間創建委託模式,以便讓子對象的委託(可能設置爲父對象)知道狀態ID已更改。

+0

你有一個代碼示例:保持從父參考子視圖控制器並調用父級的setStatusId:方法?另外,哪種方法更優雅,父代的委託模式或setStatusId方法? – 2009-09-02 17:53:52

6

與「超級」您訪問的基類,一個當前類已從

繼承做你解釋什麼,你需要訪問你的父視圖,這是因爲相當複雜的屬性這很可能以兩個班級試圖相互引用而結束。 這樣你很可能需要創建一個委託模式,有些看起來像這樣

ParentView.h

@protocol IAmYourFatherAndMotherProtocol 

@class ChildView; 

@interface ParentView : UIViewController <IAmYourFatherAndMotherProtocol> 
{ 
NSInteger statusID; 
} 

@property (nonatomic) NSInteger statusID; 

@protocol IAmYourFatherAndMotherProtocol 
@property (nonatomic) NSInteger statusID; 
@end 

@end 
在ChildView.h中

#import "ParentView.h" 

@interface ChildView : UIViewController 
{ 
    id<IAmYourFatherAndMotherProtocol> delegate; 
} 

@property (nonatomic, assign) id <IAmYourFatherAndMotherProtocol> delegate; 
創造ParentView.m您ChildView時

,您必須將「self」設置爲代表,例如:

ChildView *newChild = [[ChildView alloc] init]; 
newChild.delegate = self; 

通過這樣做,你可以ChildView.m訪問「statusID」你ParentView的是這樣的:

delegate.statusID = 1337; 

希望這有助於

+0

+1爲協議名稱 – slf 2009-09-02 17:04:59

+0

只是一個澄清。我想要做的是在包含AnnotationView的MKAnnotation視圖中更改一個屬性,該視圖具有對UIViewController的標註。我想從標註的視圖控制器中更改MKAnnotation的屬性值。 – 2009-09-02 20:19:29

+0

所以你有一個MKAnnotationView,它包含一個(nother?)(MK?)AnnotationView? 無論如何,我敢肯定,你仍然必須以類似於我發佈的方式去做。 – gabtub 2009-09-03 07:26:36