1

I have a subclass of a UIView (MyView) that has some UITextField as subviews. MyView implements the UITextFieldDelegate protocol in order to be notified when the textfields are clicked. This was working well. Now I need to place the textfields in a sort of "container" to be able to fade in and out this container (and all its children) with an UIView animation. So I created a UIView (MySubview), made it subview of MyView and placed all textfields inside of it. The animation works fine but the UITextFieldDelegate doesn't get called anymore. I think it's because the textfields are not direct children of MyView anymore. Is there any other ways to deal with this?

UPDATE

I did a small version of my code, maybe this helps to find the problem:

@interface MyView : UIView <UITextFieldDelegate>


@implementation MyView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // This is MySubview:
        UIView *tempLabelsContainer = [[UIView alloc] initWithFrame:self.bounds];
        [tempLabelsContainer setUserInteractionEnabled:YES];
        [self addSubview:tempLabelsContainer];
        self.labelsContainer = tempLabelsContainer;
        [tempLabelsContainer release];

        UITextField *aTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
        [aTextField setBackgroundColor:[UIColor clearColor]];
        [aTextField setText:@"Some text"];
        [aTextField setTag:1];
        [aTextField setDelegate:self];
        [self.labelsContainer addSubview:aTextField];
        [aTextField release];

        // More labels are being added
    }

    return self;
}

#pragma mark - UITextFieldDelegate methods

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    // This is not being called
    NSLog(@"TextField with the tag: %d should be edited", [textField tag]);    

    return NO;
}
Nilesh_iOSDev
  • 671
  • 4
  • 18
strave
  • 1,491
  • 2
  • 16
  • 26
  • Can you able to edit the text fields? – EmptyStack Sep 07 '11 at 12:04
  • Hi Sandra, I assume everything related to this written in the same controller? i.e. the MySubView is not a separate class, but just a variable inside the same controller where the UITextFields and their delegate methods are written. And of course, I'm certain you would have done the delegate assignments on the UITextFields. – Madhu Sep 07 '11 at 12:06
  • Write the delegates in a separate class file and use tat class name instead of giving self in the textfield's delegate property – stack2012 Sep 07 '11 at 12:09
  • @Madhumal Gunetileke: exactly, MySubView is not a separate class. I created an instace of UIView and placed it inside of MyView. MyView isn't a controller it's just a UIView but I'm still using it as the delegate of my textfields. It was working before I changed the view hierarchy. And yes! I did the delegate assignments on the UITextFields. – strave Sep 07 '11 at 12:38
  • @sandra, then set the `userInteractionEnabled` property of `MySubview` to `YES`. – EmptyStack Sep 07 '11 at 12:40
  • @booleanBoy: I implemented your suggestion and it didn't work. – strave Sep 07 '11 at 13:01
  • you have to create an instance of tat delegate and then assign it... – stack2012 Sep 07 '11 at 13:08

2 Answers2

4

Ok then, I took a try writing out your code, and it works out for me. Here are a few items you could try changing;

  • The reason you cannot edit your textField is because you are returning NO to the textFieldShouldBeginEditing method. Change this to YES.
  • Try positioning (if possible) your textField a little below than what you've given. Also, 20px is anyway waaaaay too small for a proper textField, make that at least 40px. A 20px textField positioned at 0,0 maybe hidden altogether if the status bar is turned on in the phone.
  • Set a borderStyle for your textField. As far as I know, this is important for a textField to show up and interact well. (e.g. textField.borderStyle = UITextBorderStyleRoundRect)

Fingers crossed! :)

Madhu
  • 2,429
  • 16
  • 31
  • Thank you so much for your detailed answer. I can't understand why it works for you and not for me :(. I tried #1, although I actually don't want to edit the text but only know when the user touches it. I didn't work. Then I tried #2 (only the height), the x and y position of my real code were different than the code I posted. It didn't work. And finally I tried #3 with no success. The strange thing is that my code was working before I added the container and changed the view hierarchy. So I was sure that it had to do with that. I'll build myself a dummy keep on testing and post again. – strave Sep 07 '11 at 15:30
0

I will answer my own question just in case someone happens to have this same problem - difficult to imagine someone doing such silly mistake, though:

I was setting the frame of the labels container to self.bounds. But the MyViewController was creating MyView with a frame of CGRectZero! I did that change to support animation at the same time I added the container. Therefore I thought that the problem had to do with the view hierarchy. Shame on me!!!

Anyway, thanks to all helpers, especially to Madhumal Gunetileke who, with his answer, made me watch in a different direction.

strave
  • 1,491
  • 2
  • 16
  • 26