2011-08-02 182 views

回答

5

的问题已经解决。我认为有人给出了很好的答案,但我忘记了。

这就是我所做的。 XIB中有一个选项。有一个复选框(标题为“启用用户交互”),指定子视图是否处理用户事件。

取消选中该复选框和所有触摸到子视图转到父视图或任何其他视图。

+1

那个复选框的名字是什么? – MartinMoizard

+0

我不再使用iPhone的代码,我忘记了。我认为这是UIControl和UIView的主要区别。 UIControl默认打开了这个复选框。 –

0

这可能帮助:

UITouch *touch = [event.allTouches anyObject]; 
CGPoint touchPoint = [touch locationInView:YOUR VIEW NAME]; 
3

检测到您需要在您的子画面或超视图(要在其中获得触摸)添加UITapGestureRecognizer触摸事件。

- (void)viewDidLoad 
{ 
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self 
                    action:@selector(tap:)]; 
     tap.numberOfTapsRequired = 1; 

     [self addGestureRecognizer:tap]; 


    [super viewDidLoad]; 
} 

UITapGestureRecognizer

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    UITouch *touch = [touches anyObject]; 
    // here you can get your touch 
    NSLog(@"Touched view %@",[touch.view class]); 
} 

那么你可以添加委托方法希望它让你的想法..

6

这为我工作:

(链路子视图在厦门国际银行或故事板)

ViewController.h

@interface ViewController : UIViewController 

@property (nonatomic, strong) IBOutlet UIView *subview; 
@property (nonatomic, strong) UITapGestureRecognizer *tapRecognizer; 

@end 

ViewController.m

@implementation ViewController 

@synthesize subview; 
@synthesize tapRecognizer; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self 
                  action:@selector(handleTap:)]; 

    [subview addGestureRecognizer:tapRecognizer]; 
} 

- (IBAction)handleTap:(UITapGestureRecognizer *)recognizer { 
    if (recognizer.state == UIGestureRecognizerStateEnded){ 
     //code here 
     NSLog(@"subview touched"); 
    } 
} 

@end 
+0

在handleTap函数中,我添加了以下行来隐藏键盘。 [self.view endEditing:YES]; –