Keyboard appears when you programatically UITextView.editible = YES;

Discussion in 'iOS Programming' started by fenrus110, Sep 7, 2008.

  1. fenrus110 macrumors regular

    Joined:
    Mar 24, 2008
    #1
    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.
     
  2. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #2
    How do you expect users to edit your text field without a keyboard?
     
  3. mccannmarc macrumors 6502

    mccannmarc

    Joined:
    Aug 15, 2008
    Location:
    Manchester, UK
    #3
    morse code with screen taps? :D lol
     
  4. fenrus110 thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #4
    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.
     
  5. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    Can you reproduce this in a separate project, using as little code as possible?
     
  6. fenrus110 thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #6
    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.
     
  7. Jeremy1026 macrumors 68020

    Jeremy1026

    Joined:
    Nov 3, 2007
    #7

    That'd be a kinda cool (free) app. I'd play with it for a couple minutes here or there.
     
  8. springframework macrumors member

    Joined:
    Mar 31, 2008
    #8
    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?
     
  9. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #9
    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.
     
  10. CyberGreg macrumors regular

    Joined:
    Jan 2, 2004
    Location:
    SoCal
    #10

    Code:
    [yourTextField resignFirstResponder];
     
  11. fenrus110 thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #11
    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.
     
  12. CyberGreg macrumors regular

    Joined:
    Jan 2, 2004
    Location:
    SoCal
    #12
    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...
     
  13. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #13
    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];
     
  14. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #14
  15. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #15
    Good find. So hopefully this gets fixed with the 2.1 update. Anyone already have it with an iPod touch and can confirm?
     
  16. fenrus110 thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #16
    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...
     
  17. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #17
    The release notes do not actually list this as a fixed bug, but a "known issue".
     
  18. zuffap macrumors newbie

    Joined:
    Jun 15, 2010
    #18
    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.
     
  19. kyocooro macrumors newbie

    Joined:
    Feb 16, 2011
    #19
    instead of using editable, you can use [textview setUserInteractionEnabled:bool];
     

Share This Page