2015-07-19 78 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