UITextView does not become the first responder

I have a UIView that contains a UITextView . UIView initiated inside the UIViewController .

But when I touch the UITextView window, nothing happens. Neither the keyboard nor the delegate methods are responsive.

The 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 the UITextView in the UIViewController and still did not respond. Neither the cursor nor the keyboard is displayed.

+6
source share
8 answers

I found a mistake.

In one class, I classify a UITextView instead of a subclass and set canBecomeFirstResponder to NO.

+4
source
 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 sentences

 self.userInteractionEnabled = YES; // superview may blocks touches self.clipsToBounds = YES; // superview may clips your textfield, you will see it 
+7
source

try the following: in your .h file there corresponds a delegate of UITextViewDelegate

 :<UITextViewDelegate> 

in your .m file:

 noteText.delegate = self; 

And delegation methods:

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

Hope this help!

+3
source

Check this:

  • add your view manager to the right of the UIWindow .

  • set the makeKeyAndVisible window and add rootViewController to the window using this method:

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

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

+2
source

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

Just in case, try setting userInteractionEnabled = YES; for your UIView object. If it is somehow set to NO, it will drain over all of its subzones.

+1
source

Perhaps you have an error in your overriden method -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event ? Maybe you do resignFirstResponder there or put some kind of view first?

+1
source

If something like this happens, check the following points,

  • Make sure the text view is correctly saved and released only in dealloc . (If you are not using ARC )
  • Check the frame of both the text view and its parent views. Make sure the subview parameter is set exactly in the parent view. Also make sure that this applies to all superviews .
  • Verify that the delegate installed correctly.

If all this is done, try adding a button on top of the text view and see if it is called by the target selector . If so, the problem is with the text view delegate or release statement. Otherwise, the problem is with setting up the Text view frame or add-in.

+1
source

I know this late, but maybe for some it will be useful. I had the same problem and it disappeared after I used

 [self setSelectable:true]; 

in my subclass of UITextView.

+1
source

Source: https://habr.com/ru/post/926704/


All Articles