2010-09-03 45 views
6

我怎麼能隱藏滾動型觸摸事件鍵盤...上滾動型觸摸辭職急救員

的情況是這樣的...

- >查看 - >滾動型 - >文本字段

我想隱藏鍵盤上的scrollView。我試圖覆蓋滾動視圖的類,但我仍然無法做到這一點。

+0

我認爲你需要使用一個委託。 – mk12 2010-09-03 04:48:03

+0

還有人可以幫我解決這個? – 2011-03-18 10:30:43

回答

8

做這樣將有助於:

@interface MyClass <UIScrollViewDelegate> { 
} 

@implementation 

- (void)viewDidLoad { 
    yourScrollView.delegate = self; 
} 

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { 
    [myTextField resignFirstResponder]; 
} 

如果你真的要處理的觸摸事件,那麼你需要繼承的UIScrollView並重寫方法:

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view { 

} 

更多UIScrollView touch

+0

我需要讓鍵盤隱藏在觸摸上,而不是拖動ScrollView ...所以有誰可以幫助我這個! – 2010-09-03 13:02:24

+0

但是,當用戶拖動,他們不得不觸摸,是不是正確 – vodkhang 2010-09-03 13:08:58

+0

我編輯我的答案,包括觸摸事件 – vodkhang 2010-09-03 13:14:26

4

試試這個:

Add gesturereco gnizer用於滾動視圖,

UITapGestureRecognizer *tapScroll = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped)]; 
    tapScroll.cancelsTouchesInView = NO; 
    [yourScrollview addGestureRecognizer:tapScroll]; 
    [tapScroll release]; 

使用(tapped :)方法將鍵盤退出。

6

這是我在你的viewController的viewDidLoad法

UITapGestureRecognizer *tapScroll = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapped)]; 
    tapScroll.cancelsTouchesInView = NO; 
    [viewScroller addGestureRecognizer:tapScroll]; 

其中viewScroller是你的滾輪什麼工作

。在抽頭方法,我們有

- (void) tapped { 
     [self.view endEditing:YES]; 
    } 

不知道爲什麼,但上面並沒有爲我工作...即使它應該

+0

是的,在iOSSDK中使用手勢後,這是一個很好的解決方案,由@ Dax解答。但是這個問題被問及很久沒有手勢的問題。但是myside的+1。 – 2012-10-22 04:29:36

0
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view 
{ 
    [self.view endEditing:YES]; 
    return YES; 
}