2011-08-02 178 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]; –