Reputation: 5510
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");
}
Upvotes: 0
Views: 233
Reputation: 131481
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?
Upvotes: 0
Reputation: 8225
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];
Upvotes: 2