2013-04-01 147 views
11

UITextField如何禁用粘貼?UITextField如何禁用粘貼?

+0

對此答案予以PengOne給出[PengOne] (http://stackoverflow.com/users/544050/pengone「PengOne」)在這個問題:http://stackoverflow.com/questions/6701019/ho w-to-disable-copy-paste-option-from-uitextfield-programmatically?rq = 1 –

+0

哦,對不起,我不會在以後重複這個問題。諾言。 – isaced

回答

75

覆蓋canPerformAction:withSender:方法返回NO不要爲您不想讓操作:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender 
    { 
     if (action == @selector(paste:)) 
      return NO; 
     if (action == @selector(select:)) 
      return NO; 
     if (action == @selector(selectAll:)) 
      return NO; 
     return [super canPerformAction:action withSender:sender]; 
    } 

在上面的代碼中,你需要編寫只爲

另一種方式

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender { 
    UIMenuController *menuController = [UIMenuController sharedMenuController]; 
    if (menuController) { 
     [UIMenuController sharedMenuController].menuVisible = NO; 
    } 
    return NO; 
} 

另請檢查This link

EDITED

在iOS中7,你可以做這樣類似,,

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender { 
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
     [[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO]; 
    }]; 
    return [super canPerformAction:action withSender:sender]; 
} 

爲SWIFT用戶

override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool { 
    if action == #selector(copy(_:)) || action == #selector(paste(_:)) { 
     return false 
    } 

    return true 
} 
+0

好的,謝謝.... – isaced

+3

不適用於iOS 7 –

+1

這不適用於iOS7。 – CMVR