2015-09-29 80 views
2

我正在使用UITextField,因爲我想要自定義彈出式鍵盤。但是,我不希望用戶能夠更改插入點或訪問複製粘貼菜單。如何禁用UITextField的選擇?

我已經發現了兩個有用的計算器問題,並試圖實現它們:

我已經通過繼承的UITextField和執行刪除菜單方法:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender { 
    return NO; 
} 

不過,我已經無法從當用戶雙擊水龍頭它被選中停止場:

我試圖消除,我認爲是負責挑選行爲gestureRecognizers,但沒有成功。那麼我做錯了什麼?

@property (nonatomic, strong) MinimalTextField *inputText; 
... 
@synthesize inputText; 
... 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear: animated];  
    NSLog(@"%ld gestureRecognizers initially ", (long)inputText.gestureRecognizers.count); 

    for (UIGestureRecognizer *gestureRecognizer in inputText.gestureRecognizers) {   
     if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) { 
      UITapGestureRecognizer *tapGestureRecognizer = (UITapGestureRecognizer *)gestureRecognizer; 
      if ([tapGestureRecognizer numberOfTapsRequired] == 2) { 
       NSLog(@"found & removed: %@", tapGestureRecognizer); 
       [inputText removeGestureRecognizer:tapGestureRecognizer]; 
      } 
     } 
     if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) { 
      UILongPressGestureRecognizer *longPressGestureRecognizer = (UILongPressGestureRecognizer *)gestureRecognizer; 
      NSLog(@"found & removed: %@", longPressGestureRecognizer); 
      [inputText removeGestureRecognizer:longPressGestureRecognizer]; 
     } 
    } 

    NSLog(@"%ld gestureRecognizers remaining", (long)inputText.gestureRecognizers.count); 

    for (UIGestureRecognizer *gestureRecognizer in inputText.gestureRecognizers) { 
     NSLog(@"gestureRecognizer: %@", gestureRecognizer); 
    } 
} 

此代碼產生以下輸出,所以我知道它正在工作,但它不會影響雙擊操作。

7 gestureRecognizers initially 
found & removed: <UITextTapRecognizer: 0x7ff6086571f0; state = Possible; delaysTouchesEnded = NO; view = <MinimalTextField 0x7ff608414b10>; target= <(action=oneFingerDoubleTap:, target=<UITextInteractionAssistant 0x7ff608652de0>)>; numberOfTapsRequired = 2> 
found & removed: <UILongPressGestureRecognizer: 0x7ff608658180; state = Possible; delaysTouchesEnded = NO; view = <MinimalTextField 0x7ff608414b10>; target= <(action=twoFingerRangedSelectGesture:, target=<UITextInteractionAssistant 0x7ff608652de0>)>> 
found & removed: <UIVariableDelayLoupeGesture: 0x7ff608658a40; state = Possible; delaysTouchesEnded = NO; view = <MinimalTextField 0x7ff608414b10>; target= <(action=loupeGesture:, target=<UITextInteractionAssistant 0x7ff608652de0>)>> 

4 gestureRecognizers remaining 
gestureRecognizer: <UITextTapRecognizer: 0x7ff608653960; state = Possible; delaysTouchesEnded = NO; view = <MinimalTextField 0x7ff608414b10>; target= <(action=oneFingerTripleTap:, target=<UITextInteractionAssistant 0x7ff608652de0>)>; numberOfTapsRequired = 3> 
gestureRecognizer: <UITextTapRecognizer: 0x7ff6086576e0; state = Possible; delaysTouchesEnded = NO; view = <MinimalTextField 0x7ff608414b10>; target= <(action=twoFingerSingleTap:, target=<UITextInteractionAssistant 0x7ff608652de0>)>; numberOfTouchesRequired = 2> 
gestureRecognizer: <UITapAndAHalfRecognizer: 0x7ff608657c70; state = Possible; view = <MinimalTextField 0x7ff608414b10>; target= <(action=tapAndAHalf:, target=<UITextInteractionAssistant 0x7ff608652de0>)>> 
gestureRecognizer: <UITextTapRecognizer: 0x7ff6086585f0; state = Possible; delaysTouchesEnded = NO; view = <MinimalTextField 0x7ff608414b10>; target= <(action=oneFingerTap:, target=<UITextInteractionAssistant 0x7ff608652de0>)>> 

我甚至嘗試添加以下代碼,以我的UITextField的子類:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer 
{ 
    NSLog(@"Gesture should begin"); 
    if ([gestureRecognizer isKindOfClass:[UIRotationGestureRecognizer class]]) 
     NSLog(@"rotate"); 
    if ([gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]]) 
     NSLog(@"pinch"); 
    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) { 
     NSLog(@"tap"); 
     NSLog(@"numberOfTouches: %ld", (long)gestureRecognizer.numberOfTouches); 
    } 
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) 
     NSLog(@"pan"); 
    if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) 
     NSLog(@"long"); 
    if ([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]]) 
     NSLog(@"swipe"); 
    return YES; 
} 

然而,沒有物業gestureRecognizer.numberOfTaps可用,所以我怎麼能知道它是如何經常被竊聽。

回答

0

UITextView中有一個屬性selectable,可以幫助您

+1

這是正確的,我可能最終使用的UITextView。不幸的是,我寫了這個問題的錯誤標題,我剛剛修改了它。 – Steve

+0

讓我重新說一下我的回答。我仍然有同樣的問題,我不知道如何禁用UITextField的選擇。在最初的問題中,當我第一次發佈它時,我錯誤地寫了UITextView而不是UITextField,我編輯它來糾正這個錯誤。我需要在我的代碼中使用UITextField,但不幸的是它沒有可選的屬性。 – Steve

+0

不知道如何做到這一點,但如果您通過在其上放置一個調用'[textField becomeFirstResponder]的按鈕來禁用UITextField上的所有點擊,就可以對其執行操作 – Leonardo