2015-07-19 83 views
0

我有一個superview包含一個childview。如何聆聽兒童視圖IBAction?

在childview中,我有一些按鈕連接到childview中的操作。

我想讓我的超級視圖也響應這些按鈕事件,我該怎麼做?

SuperView.m 

@property (strong, nonatomic) ChildView *childView; 

//view did load 
childView = [[ChildView alloc] init]; 


ChildView.m 

- (IBAction)someButtonTouched:(id)sender { 
    //Event Handling in ChildView 
} 
+0

使用委派確保üremoveObservers。 – Vakas

回答

2

在你的情況,考慮MVC規則(即ü想打破),我建議ü使用NSNotificationCenter.

所以,只要postNotification當childView按下按鈕:

[[NSNotificationCenter defaultCenter] postNotificationName:@"buttonPressedOnChildView" object:self userInfo:nil]; 

而在你superViewü應該addObserver這樣的:

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(doButtonPressedInChildView:) 
              name:@"buttonPressedOnChildView" 
              object:nil]; 

不要忘了在superView添加方法:

-(void)doButtonPressedInChildView:(NSNotification*)notification { 
    // Do something in superView 
} 

superViewviewDidDisapear:

-(void)viewWillDisppear:(BOOL)animated { 
    [super viewWillDisppear:animated]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 

} 
+0

謝謝@ gran33,這是我想要的。 –

+0

享受!請將此答案標記爲答案:) – gran33