iOS Keyboard appears when you programatically UITextView.editible = YES;

fenrus110

macrumors regular
Original poster
Mar 24, 2008
142
0
I have a UITextView that starts off with editable == NO. In my code, I programatically set editable = YES. (let's say a button press triggers it)

When that happens, the keyboard pops up for some reason, why is that? I tried:
UITextView.editable = YES;
[UITextView resignFirstResponder];

but it has no affect in getting rid of the keyboard. I even NSLogged my textviewDidBegin/ShouldBegin Editing, but it appears the code doesn't even go there.

My delegates respond fine though if the UITextview starts off as editable and I don't mess with that attribute.
 

fenrus110

macrumors regular
Original poster
Mar 24, 2008
142
0
well normal behaviour for a UITextView's keyboard is suppose to pop up when you tap the UITextView's area. It's not suppose to (or shouldn't) pop up when I type in code that does ".editible = YES;"

and since .enabled isn't an attribute of UITextView, changing the .editible is the only way to change it from read or read/write.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,745
3
Can you reproduce this in a separate project, using as little code as possible?
 

fenrus110

macrumors regular
Original poster
Mar 24, 2008
142
0
yes, the same thing happens if I use a minimal project on both simulator and the actual iPhone

Essentially just put a UIButton and UITextView, use this in the handler (useless code, but shows the example)

Code:
-(IBAction) touchButton:(id)sender {
   // tv is a UITextView
   if (tv.editible == NO)
      tv.editable= YES;
   else
      tv.editable = NO;
}
when you start clicking on the button, the keyboard will pop up. But from my experiences with UITextView, the keyboard should only pop up when the user touches the area of the UITextview and when .editible == YES

somehow the keyboard gets focus without triggering my UITextView's "shouldBegin/didBegin" delegates.
 

springframework

macrumors member
Mar 31, 2008
59
0
hey fenrus110,

i tried out your problem and you are right. what a stupid bug.

i tried all kinds of things to get rid of it but all without success so far.

i even dug into the subviews and found that the UITextView contains a mysterious undocumented class called 'UIWebDocumentView'

strange stuff. it seems as if apple forgot to perform some simple tests on their components...?



Does anyone know a ways that you can hide the keyboard with some code?
 

kainjow

Moderator emeritus
Jun 15, 2000
7,745
3
I'd report it as a bug if it's not documented to behave like this.

I think UITextView uses a UIWebView to render its text. If you look at its ivars it has several WebKit classes.
 

fenrus110

macrumors regular
Original poster
Mar 24, 2008
142
0
Code:
[yourTextField resignFirstResponder];
doesn't work. I put in:

{
tv.editable = YES;
[tv resignFirstResponder];
}

As I said, the UITextView isn't responding to my delegates, so that means it didn't call the keyboard, so resigning it won't do anything.
 

CyberGreg

macrumors regular
Jan 2, 2004
135
0
SoCal
doesn't work. I put in:

{
tv.editable = YES;
[tv resignFirstResponder];
}

As I said, the UITextView isn't responding to my delegates, so that means it didn't call the keyboard, so resigning it won't do anything.
It works fine in my code but I have a button that will hide the keyboard, so it wouldn't be in the definition of your text field as you have displayed. I am also not using the 'editable' property. I took my code from the Hello World example.
Sorry, I'm no help...
 

kainjow

Moderator emeritus
Jun 15, 2000
7,745
3
I think I found a fix, although it uses a private undocumented method, but it's harmless.

Subclass UITextView and implement this method:
Code:
- (void)performBecomeEditableTasks {}
By overriding this method it seems that you're disabling the text view becoming the first responder when editable becomes YES. So it doesn't look like a bug, but I bet they did this because there's no other visual indication that the view is enabled or disabled, unlike normal text field controls on the Mac.

Also when you set editable to NO, first call resignFirstResponder to hide the keyboard and remove the text field as the first responder. If you don't do this, the behavior of the text view goes out of whack.

Edit: if the delegate methods aren't getting called, then that would be a bug. Also if you don't want to use the private method workaround, you can call this immediately after setting editable to YES and the keyboard will hide (although it still shows up for a split second):
Code:
[tv performSelector:@selector(resignFirstResponder) withObject:nil afterDelay:0.0];
 

kainjow

Moderator emeritus
Jun 15, 2000
7,745
3
Good find. So hopefully this gets fixed with the 2.1 update. Anyone already have it with an iPod touch and can confirm?
 

fenrus110

macrumors regular
Original poster
Mar 24, 2008
142
0
Good find. So hopefully this gets fixed with the 2.1 update. Anyone already have it with an iPod touch and can confirm?
I installed 2.1 on my iPod Touch, got the new SDK and compiled under 2.1 Device and Simulator, I still see the bug happening.

.editible=YES; // brings up keyboard, ignores delegates
.editible=NO; // hides keyboard, ignores delegates

Release Notes 2.1 do say:
Setting UITextView.editable to YES should not automatically show the keyboard.

But it doesn't seem to be happening...
 

Luke Redpath

macrumors 6502a
Nov 9, 2007
731
6
Colchester, UK
I installed 2.1 on my iPod Touch, got the new SDK and compiled under 2.1 Device and Simulator, I still see the bug happening.

.editible=YES; // brings up keyboard, ignores delegates
.editible=NO; // hides keyboard, ignores delegates

Release Notes 2.1 do say:
Setting UITextView.editable to YES should not automatically show the keyboard.

But it doesn't seem to be happening...
The release notes do not actually list this as a fixed bug, but a "known issue".
 

zuffap

macrumors newbie
Jun 15, 2010
1
0
Workaround: two parallel textviews

I created a workaround to this bug that works until Apple fixes the bug:
simply create another UITextView with exactly the same position as the original one and set it's "editable" property to NO at the beginning.
Then instead of toggling the "editable" property of the original text view just swap those two text views by toggling their "hidden" property.
Do not forget to copy the text from the original text view into the disabled one.