0

The question: How do I prevent the copy/paste/select popup that occurs over a UITextView from appearing (not using UIwebView and css)?

I did not want to go the rout of UIWebView as some posts have gone because I already am using UIViews with UITextFields for data entry. I had tried unsuccessfully to implement the solutions dealing with UITextField in my implementation file of my view controller with the methods: targetForAction:withSender, setMenuVisible:animated and finally canPerformAction:withSender. (It NO WORKY WORKY - [sad face])

tony.stack
  • 780
  • 8
  • 21
  • This isn't a question. – rmaddy Jul 01 '14 at 16:27
  • reformatted with my original question. thanks! – tony.stack Jul 01 '14 at 17:04
  • 1
    Your "question" is still more of an answer. If your goal is to answer your own "question" because you think it will help other people than split it up into an actual question and post an actual answer below. – rmaddy Jul 01 '14 at 17:07
  • Ok -- I posted the solution to my dilemma below. i hope this is clearer and helps some folks in their adventures in iOS land! – tony.stack Jul 01 '14 at 17:15

2 Answers2

0

Ok, I found a working solution (in Xcode 5.1) to my question which, in short, is subclassing the UITextField.

I realized I wasn't really overriding the default behavior of the UITextField in the view controller like I wanted to and neither was putting the methods listed here override the behavior of the textfield delegate in the view controller file. The Key was to subclass the UITextField itself with -targetForAction:withSender. (I know some of you are screaming at the screen about how OBVIOUS that was!) It was not obvious to me. Like most problems when first figuring them out I went through a lot of different paths some I found here in SO. But the solution is a simple one. I want to share this solution in its own area so hopefully it can help someone out.

The header file:

//
//

#import <UIKit/UIKit.h>

@interface TPTextField : UITextField

- (id)targetForAction:(SEL)action withSender:(id)sender;
@end

and the implementation file (.m)

//
//

#import "TPTextField.h"

@implementation TPTextField

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
#pragma mark - method overrides - deny user copy/paste on UITTextFields
- (id)targetForAction:(SEL)action withSender:(id)sender
{
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    if (action == @selector(selectAll:) || action == @selector(paste:) ||action == @selector(copy:) || action == @selector(cut:)) {
        if (menuController) {
            [UIMenuController sharedMenuController].menuVisible = NO;
        }
        return nil;
    }
    return [super targetForAction:action withSender:sender];
}

@end

In your storyboard or nib/xib file just connect this class to your UITextfield like the picture below:

Custom Class:TPTextfield

I have it on git to for easy access here. Please let me know if this is helpful to you!

Tony

tony.stack
  • 780
  • 8
  • 21
  • Actually, this is exactly what `canPerformAction:withSender:` is for. You should not implement `targetForAction:withSender:`. – quellish Jul 01 '14 at 19:13
  • What is the reason you should not use targetForAction:withSender:? @quellish – tony.stack Jul 01 '14 at 19:22
  • The UIResponder documentation. `canPerformAction:withSender:` allows customization of the menu commands. That is how the application knows wether the edit menu commands should be available in a given context. `targetForAction:withSender:` is what connects a command with a target - AFTER `canPerformAction:withSender:` is called to validate that the command should be available. You want to make the menu commands not available - `canPerformAction:withSender:` is the correct way to do that. – quellish Jul 01 '14 at 20:00
0

If the UITextView is created as an object on a storyboard, the solution is even easier. In Attributes Inspector for the UITextView object, under Behavior, uncheck Editable and uncheck Selectable. Under the Scroll View section, you can check Scrolling Enabled if you want the user to be able to scroll text.

GlennRay
  • 959
  • 9
  • 18