0

I would like to intercept a touch on a UITextField, for instance when I first load it I give it a @selector() to load a different method instead of the delegate stuff?

This is my attempt:

descriptionText = [[UITextField alloc] initWithFrame:CGRectMake(10.0, 25.0, infoView.frame.size.width - 20, 100.0)];
    descriptionText.backgroundColor = [UIColor whiteColor];
    descriptionText.textColor = [UIColor blackColor];
    descriptionText.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
    descriptionText.userInteractionEnabled = YES;
    [descriptionText addTarget:self action:@selector(loadInfoView:) forControlEvents:UIControlEventTouchUpInside];
    descriptionText.font = [UIFont fontWithName:@"Helvetica" size:15];
    // show view
    [infoView addSubview:descriptionText];

However when I debug the method:

- (void)loadInfoView
{
    NSLog(@"load other view here");
}
halfer
  • 19,824
  • 17
  • 99
  • 186
HurkNburkS
  • 5,492
  • 19
  • 100
  • 183

2 Answers2

2

Your problem is in the forControlEvents:: try UIControlEventEditingDidBegin

[descriptionText addTarget:self action:@selector(loadInfoView:) UIControlEventEditingDidBegin];

Your @selector(loadInfoView:) is also wrong if you have - (void)loadInfoView

or @selector(loadInfoView:) and - (void)loadInfoView: (id) sender
or @selector(loadInfoView) and - (void)loadInfoView

However, why you don't use the UITextFieldDelegate?

.m

@interface ViewController () <UITextFieldDelegate>

    @property (strong, nonatomic) UITextField *descriptionText;

@end

Remember to:

self.descriptionText.delegate = self;

Then:

-(void) textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == self.descriptionText)
    {
        [self loadInfoView];
    }

}

Keyboard:

If you don't want to show the keyboard you need to add a [descriptionText resignFirstResponder];

Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
0

I'm not sure you can get actions to be called on a text field on touch like you want.

If that doesn't work, why don't you attach a tap gesture recognizer to your text field?

Duncan C
  • 128,072
  • 22
  • 173
  • 272