如何从IBAction方法访问视图

时间:2011-09-10 09:31:41

标签: iphone objective-c cocoa-touch

我在Windows中有4个视图。所有视图链接到同一方法 - (IBAction)触及:(id)发件人

如何在视图1中触摸时仅视图1背景颜色发生变化。 但左边3视图不是。 另外,如果我触摸另一个视图,则仅触摸更改背景颜色,但其他视图不会更改。

谢谢

2 个答案:

答案 0 :(得分:1)

您应该实施-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

UIViewController方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    if([event touchesForView:view1] != nil) 
        //View1 touched
    else if ([event touchesForView:view2] != nil) 
        //View2 touched
    else if ([event touchesForView:view3] != nil) 
        //View3 touched
    else if ([event touchesForView:view4] != nil) 
        //View4 touched 
}

答案 1 :(得分:0)

您可以通过设置其“标记”属性来标记您的视图,并在您的(IBAction)触摸:( id)发件人中检查标记值,如

if (((UIView*)sender).tag == 1) {
    //Do something for the view with tag value 1
}