Question

i have a problem. I have implemented an UITextField that contains a phonenumber, then i have implemented the following method to call the phone number:

- (void)rufeAn{
NSString *prefix = (@"tel://");
UIApplication *app = [UIApplication sharedApplication];
NSString *dialThis = [NSString stringWithFormat:@"%@%@", prefix, map.kordinate.telefon];
NSURL *url = [NSURL URLWithString:dialThis];
[app openURL:url];

}

But how can i say to the textField when i touch on it, to start the method and dial?

Was it helpful?

Solution

I agree with St3fan that an normal TextField should never cause an action by tapping. I will assume for the moment that you have a special UITextField that can be made uneditable and then looks like a UILabel, formatted to look like a hyperlink. Such a UI could be correct. Tapping a text field that looks like it's editable but actually makes a phone call would of course be incorrect.

You could implement the UITextFieldDelegate protocol and watch for textFieldShouldBeginEditing:. Since this text field is not editable, obviously you should return NO.

You could add a "call" button inside of the text field using its overlay views (leftView or rightView; read the docs on the correct UE for these).

You could subclass UITextField to override touchesBegan:withEvent, touchesEnded:withEvent: and touchesCancelled:withEvent: to handle the touch yourself. Remember: you must receive a touchesBegan and touchesEnded without a touchesCancelled between them in order to consider it a touch. Never fire an action based only on touchesBegan.

OTHER TIPS

Tapping a text field should never result in some action. In my opinion that is bad user interface design. Why don't you add a button next to the field that runs the above code?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top