Objective c 如何获取子视图上的触摸事件并根据它委派适当的操作

Objective c 如何获取子视图上的触摸事件并根据它委派适当的操作,objective-c,Objective C,我是iphone开发的新手,这是我的第二个示例代码。 我试图将子视图添加到视图中,并根据所接触的视图生成事件。我正在试验的项目是一个新创建的、干净的基于窗口的应用程序 我在唯一的viewController代码中编写了以下代码: @interface testViewController : UIViewController { IBOutlet UIView *redView; IBOutlet UIView *redView1; IBOutlet UIView *b

我是iphone开发的新手,这是我的第二个示例代码。 我试图将子视图添加到视图中,并根据所接触的视图生成事件。我正在试验的项目是一个新创建的、干净的基于窗口的应用程序

我在唯一的viewController代码中编写了以下代码:

@interface testViewController : UIViewController {

    IBOutlet UIView *redView;
    IBOutlet UIView *redView1;
    IBOutlet UIView *blueView;

}

//---expose the outlet as a property---
@property (nonatomic, retain) IBOutlet UIView *redView;
@property (nonatomic, retain) IBOutlet UIView *redView1;
@property (nonatomic, retain) IBOutlet UIView *blueView;

//---declaring the action---
-(IBAction) viewClicked: (id) sender;

@end
它的.m文件包含一个动作响应程序。我试图做的是,当我的视图被触摸时,它们将生成一个事件,该事件应通过此单一方法处理,该方法将相应地更改红色视图的背景色

-(IBAction) viewClicked:(id) sender {
    redView.backgroundColor = [UIColor blackColor];
}
我用这种方式说服代表

    @interface testAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    testViewController *viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet testViewController *viewController;

@end

谢谢

您是如何将viewClicked链接到视图的?通常,要处理触摸,请使用touchesbreated:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch* touch = [touches anyObject];
           NSUInteger numTaps = [touch tapCount];
    if ([touches count] > 1)
        NSLog(@"mult-touches %d", [touches count]);

        if (numTaps < 2) {
    } else {
        NSLog(@"double tap");
    }

}
-(void)touchesbeated:(NSSet*)toucheevent:(UIEvent*)event{
UITouch*touch=[触摸任何对象];
NSU整数numTaps=[touch tapCount];
如果([触摸计数]>1)
NSLog(@“多次触摸%d”,[触摸计数]);
如果(numTaps<2){
}否则{
NSLog(“双击”);
}
}

您好,谢谢您的回答,但我已经更新了我的问题,希望它现在很容易理解。您的每个视图都应该有一个touchesbearth:然后将调用被触摸视图的touchesbearth方法,而不是viewController。如果希望viewController完成所有工作,则需要一个hitTest:withEvent:。您可能希望将此标记为iphone问题,以便iphone窥视者看到它。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch* touch = [touches anyObject];
           NSUInteger numTaps = [touch tapCount];
    if ([touches count] > 1)
        NSLog(@"mult-touches %d", [touches count]);

        if (numTaps < 2) {
    } else {
        NSLog(@"double tap");
    }

}