2012-07-07 80 views
0

視圖獲取觸摸我想有使用的頂視圖(UIView的子類),它捕捉觸摸與子視圖

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
... 

.H

@interface XView : UIView<UITableViewDataSource, UITableViewDelegate> 

@property (nonatomic, strong) UITableView * tableView; 

@end 

這工作得很好,如果視圖是空的,但只要我插入(addSubview)可以說一個UITableView

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 

     self.tableView = [[UITableView alloc] initWithFrame:self.frame]; 

     self.tableView.delegate = self; 
     self.tableView.dataSource = self; 

     [self addSubview:self.tableView]; 

    } 
    return self; 
} 

比內XVIEW不trigge觸摸方法紅色

回答

0

你可能需要繼承你的UITableView或使用UITapGestureRecognizer來解決這個問題。我個人會使用UITapGestureRecognizer。

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCode:)]; 
[self.tableView addGestureRecognizer:tap]; 

,然後你可以處理你的水龍頭在這裏:

- (void)tapCode:(UITapGestureRecognizer *)recognizer 
{ 
    // code goes here 
} 

如果子類的UITableView,你可以把你的touchesBegan:withEvent在那裏。