is there a way that pa开发者_开发技巧rt of the text that is being displayed in an iphone UIAlertView will be a phone number that when clicked will be dialed? maybe using tel: somehow ?
If you like to implement the dataDetectorType in your message text there is no native way to do it. The only way is to subclass the UIAlertView and customize the init method like this :
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {
self = [super initWithTitle:title message:nil delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil];
if (self) {
CGRect alertFrame = [self frame];
UITextView myTextView = [[UITextView alloc] initWithFrame:CGRectMake(alertFrame.origin.x + 10, alertFrame.origin.y + 44, 200, 44)];
[myTextView setEditable:NO];
[myTextView setBackgroundColor:[UIColor clearColor]];
[myTextView setDataDetectorTypes:UIDataDetectorTypeAll];
[myTextView setText:@"http://www.apple.com"]; // Use your original message string from init
[self addSubview:myTextView];
[myTextView release]
}
return self;
}
I tested it right now and it works but you need to spend a little bit to make it presentable :P
Maybe using the way posted by Jhaliya is quickly and more clean.
Yes, This is possible by setting the delegate ( UIAlertViewDelegate ) of your UIAlertView. Read the message and title from your UIAlertView by using below properties.
@property(nonatomic, copy) NSString *message
@property(nonatomic, copy) NSString *title
You need to implement the delegte method in order to get the info for which button pressed.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
you could also follow link for getting pressed button index and dial an Phone number by Using tel: .
So, your code could be something like below .
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//Access `title` and `message` of your **UIAlertView**
NSString* alertTitle = alertView.title;
NSString* alertMessage = alertView.message;
// Formatted the phone number and assign it to a string.
NSString* myFormattedPhNumber = /*Use StringWithFormat function of NSString */;
if (buttonIndex == 0)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:myFormattedPhNumber];
}
}
精彩评论