1

I have added a textfield in oe of the cell in my tableview. Now I want to compare if the touched object is textfield or not. For that I am using -

  -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event
    {
       UITouch *touch = [[event allTouches] anyObject];
    }

method. Here I have to get the view touched in UITableview cell is textfield. How can I get that? I am struct at this. Please help.

Darshan Kunjadiya
  • 3,323
  • 1
  • 29
  • 31
Juno
  • 347
  • 2
  • 11
  • Why do you want to get the touch in touchesBegan: method? Why not using outlet for textfield or delegate methods of uitextfield? – Dinesh Raja Aug 05 '13 at 12:13

3 Answers3

1

Use TextField Delegate method :

-(void)textFieldDidBeginEditing:(UITextField *)textField
Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33
1

I think the best way to achieve this would be:

  1. Create a UITableViewCell subclass with a UITextField as a subview. Define it as an IBOutlet and hook it up via interface builder.
  2. Create a delegate protocol to notify a delegate (in your case it could be the same class handling the UITableViewDataSource) of this event.
  3. Declare the UITableViewCell subclass as a UITextFieldDelegate and hook it up to the UITextField you've created. via IB
  4. Implement

    -(void)textFieldDidBeginEditing:(UITextField *)textField { [self.delegate textFieldWasTapped]; }

Now you'll be notified in the main controller of a textFieldWasTapped event. There's no need to check if it indeed came from a UITextField object because only instances if this type will be able to trigger this call.

Stavash
  • 14,244
  • 5
  • 52
  • 80
0
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event
{
    UITouch *touch = [[event allTouches] anyObject];
    if([[touch view] isKindOfClass:[UITextField class]])
    {
         UITextField *txtField = (UITextField *) [touch view];
         //UITextField object
    }
}
Rahul Mane
  • 1,005
  • 18
  • 33