6

I have a UIView that contains a UITextView. The UIView is initiated inside a UIViewController.

But when I touch the UITextView box, nothing happens. Neither the keyboard appears nor delegate methods respond to interaction.

Code:

    noteText = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 700, 240)];
    noteText.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2].CGColor;
    noteText.layer.borderWidth = 2;
    [noteText setEditable:YES];
    noteText.userInteractionEnabled = YES;
    noteText.returnKeyType = UIReturnKeyDone;
    noteText.font = [UIFont fontWithName:@"Calibri" size:16];
    noteText.textColor = [UIColor blackColor];
    [self addSubview:noteText];

Update 1

I removed all other views from the UIViewController, and only put a UITextView in the UIViewController and still not reacting. Neither cursor or keyboard appear.

iDev
  • 23,310
  • 7
  • 60
  • 85
lagos
  • 1,968
  • 3
  • 17
  • 26
  • 2
    Make sure "user interaction enabled" is checked on view attributes at interface builder. – Firula Oct 01 '12 at 21:15
  • I don't use IB, but I have setted [textview setUserInteractionEnabled:YES] and [textview setEnabled] – lagos Oct 01 '12 at 21:17
  • did you tried [textview setDelegate:self]; ? – Firula Oct 01 '12 at 21:24
  • What are the dimensions of its superview? – Carl Veazey Oct 01 '12 at 21:33
  • The textview will open the keyboard automatically when you touch it, that should react even that I don't have set the delegate. But I have set the delegate from the UIViewController. Since the must part of the logical of the app happen in the uiviewcontroller. – lagos Oct 01 '12 at 21:34
  • @CarlVeazey the dimension of the UIView is 40, 80, 750, 250. So is sligthly bigger. The border of the textview is shown inside the size of the UIView. – lagos Oct 01 '12 at 21:37
  • Did you add another subview to the UITextView's superview after the text view is added? – geraldWilliam Oct 02 '12 at 02:27
  • Not in the UIView. The UIView contains only this UITextView. But the UIViewController has anothers subviews, but not in that frame. And the switching between subviews is controlled by a custom segmentedcontroller and all the other views is hidden when is not selected. – lagos Oct 02 '12 at 06:38
  • Seems like there could be another view's frame on top of the textView that's intercepting touch events. – Daddy Oct 04 '12 at 12:47
  • 1
    Try subclassing the text view and overriding touchesBegan: Then try subclassic the superview and in its touchesBegan: try to find which view is getting the touches using hitTest: ? – Abhinit Oct 04 '12 at 12:56
  • Now I have had remove all the others subview from superview, I'd subclassed the textview and overwritten touchesBegan:withEvent: class, and overwritten the same method in the UIView. Nothing works. I even bring it to the front. If I put some text string in the text appears, but I cant write. – lagos Oct 04 '12 at 13:13
  • what size of UIView that contains a UITextfield? – NeverBe Oct 06 '12 at 18:18
  • The UIView have the frame coordinates: `40, 80, 750, 250` and the frame of the UITextview is `0,0,700,240` – lagos Oct 08 '12 at 08:26
  • Are you releasing the UITextView after adding as subview of UIView? Try commenting out that portion and check it. Could be a release issue also. – iDev Oct 10 '12 at 08:25
  • I use ARC. So I don't need to release. – lagos Oct 10 '12 at 09:23

8 Answers8

7
noteText = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 700, 240)];
noteText.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2].CGColor;
noteText.layer.borderWidth = 2;
[noteText setEditable:YES];
noteText.userInteractionEnabled = YES;
noteText.returnKeyType = UIReturnKeyDone;
noteText.font = [UIFont fontWithName:@"Calibri" size:16];
noteText.textColor = [UIColor blackColor];
[self addSubview:noteText];

// Two suggestions

self.userInteractionEnabled = YES; // superview may blocks touches
self.clipsToBounds = YES; // superview may clips your textfield, you will see it
NeverBe
  • 5,213
  • 2
  • 25
  • 39
  • 2
    In my case it was exactly that, the UITextField superview was being resized by autolayout but I couldn't notice it because it wasn't clipping to bounds. – Marco Pompei Nov 13 '13 at 11:03
  • In my case it was also that AutoLayout compressed the view, but because clipsToBounds was No, it showed anyway. Thanks. – Scott Allen Mar 05 '14 at 19:07
  • for me adding `textView.userInteractionEnabled = true textView.editable = true` worked, I thought this was default behavior, guess it's possible it was getting set somewhere I'm unaware of – Ethan Parker Jan 17 '17 at 22:56
4

I found the error.

In one class I categorize the UITextView instead of subclass and set canBecomeFirstResponder to NO.

lagos
  • 1,968
  • 3
  • 17
  • 26
3

try this: in your .h file conform the delegate UITextViewDelegate

:<UITextViewDelegate>

in your .m file:

noteText.delegate = self;

And delegate methods:

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    [textView becomeFirstResponder];
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    //resign for exapmple
    return YES;
}

Hope this help!

lagos
  • 1,968
  • 3
  • 17
  • 26
donjordano
  • 346
  • 3
  • 12
  • 2
    the builtin `becomeFirstResponder` calls `textFieldShouldBeginEditing:` so this will cause an infinite loop – Tin Can Jun 19 '13 at 13:52
3

I know it's late, but maybe for someone it'll be helpful. I had the same problem, and it disappeared after I used

[self setSelectable:true];

in my UITextView subclass.

Artem
  • 391
  • 3
  • 11
2

Check this things:

  1. add your viewcontroller on the right UIWindow.

  2. set window makeKeyAndVisible and add rootViewController to window at this method:

    -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    I think here is something wrong in xib file or at appdelegate.

Danil
  • 1,780
  • 17
  • 24
  • I don't know what you means here. Is not a problem to load the rootviewcontroller in UIWindow. Neither do I use xib-files. – lagos Oct 08 '12 at 08:24
1

Try overriding - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { inside the UIView to see if it's receiving touches

Just in case, try setting userInteractionEnabled = YES; for your UIView object. If it's somehow set as NO it will trickle down to all of its subviews

Daddy
  • 9,045
  • 7
  • 69
  • 98
1

Maybe you have some mistake in your overriden -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event method? Maybe you do resignFirstResponder there or put some view above all?

Anton K
  • 326
  • 1
  • 13
  • For the moment the method only looks like this for see if the view react: `-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"Received Touch!"); } ` – lagos Oct 08 '12 at 11:58
  • Do you use `[self addSubview:noteText];` or `[self.view addSubview:noteText];`? – Anton K Oct 09 '12 at 04:07
  • Since noteText that is a UITextView is subview of the UIView class named NoteView. There I use `[self addSubview:noteText]`, NoteView is subviewed in the UIViewController and there I use `[selv.view addSubview:noteView]` – lagos Oct 09 '12 at 11:22
  • Does cursor appear when you tap on you textView? – Anton K Oct 10 '12 at 06:18
  • Nope. Even the keyboard won't appear. But if I hard code text, the string appear in the textview. – lagos Oct 10 '12 at 07:48
  • I think that some view lay above your textView. try to use `[self bringSubviewToFront:yourTextView]` and also bring all views which content yourTextView to front. – Anton K Oct 10 '12 at 07:59
  • Look at my last update. Removed all other views and still not working. Have already tried bringSubviewToFront: – lagos Oct 10 '12 at 08:15
  • how you initialize your ViewController? – Anton K Oct 10 '12 at 08:21
1

Whenever something like this happens check the following items,

  1. Make sure that the Text view is properly retained and released only in dealloc.(If you are not using ARC)
  2. Check the frame of both Text view and it's parent views. Make sure that the subview is fitting exactly inside the parent view. Also make sure that this is the case with all the superviews of text view.
  3. Check whether the delegate is set properly.

If all these are done, try adding a button on top of text view and check if it's target selector is getting called. If yes, the issue is with text view delegate or release statement. Otherwise the issue is with frame setting of Text view or it's superviews.

iDev
  • 23,310
  • 7
  • 60
  • 85