问题描述
|
是否有办法将iphone UIAlertView中显示的部分文本作为电话号码,单击该电话号码便会拨号?
也许使用电话:以某种方式?
解决方法
如果要在消息文本中实现dataDetectorType,则没有本机的方法。
唯一的方法是子类化UIAlertView并自定义init方法,如下所示:
- (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;
}
我现在已经对其进行了测试,并且可以正常工作,但是您需要花一点时间使其具有外观:P
也许使用Jhaliya发布的方法可以更快更干净。
, 是的,这可以通过设置UIAlertView的委托(UIAlertViewDelegate)来实现。使用以下属性,从UIAlertView中读取消息和标题。
@property(nonatomic,copy) NSString *message
@property(nonatomic,copy) NSString *title
您需要实现delegte方法才能获取按下按钮的信息。
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
您也可以点击链接以获得按下按钮的索引,并使用tel:拨打电话号码。
因此,您的代码可能类似于以下内容。
- (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];
}
}