Objective c ui按钮作为MKAnnotationView中的子视图不';行不通
我已经在注释视图中添加了一个按钮,它显示正确。当点击按钮时,我想打电话,但当我点击按钮时,它没有响应(按钮甚至没有高亮显示)。按钮似乎没有收到触摸事件,但我不知道为什么 这是我在customAnnotationView.m中的代码Objective c ui按钮作为MKAnnotationView中的子视图不';行不通,objective-c,ios,mkannotation,Objective C,Ios,Mkannotation,我已经在注释视图中添加了一个按钮,它显示正确。当点击按钮时,我想打电话,但当我点击按钮时,它没有响应(按钮甚至没有高亮显示)。按钮似乎没有收到触摸事件,但我不知道为什么 这是我在customAnnotationView.m中的代码 - (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier { NSLog(@"initWithAnnotat
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
NSLog(@"initWithAnnotation start");
//initialze the view
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self != nil)
{
CGRect frame = self.frame;
frame.size = CGSizeMake(60.0, 85.0);
self.frame = frame;
self.backgroundColor = [UIColor clearColor];
self.centerOffset = CGPointMake(30.0, 42.0);
}
//this has nothing to do with my question
self.title=[[[NSString alloc] init] autorelease];
self.description=[[[NSString alloc] init] autorelease];
self.phoneNumber=[[[NSString alloc] init] autorelease];
customAnnotation * tempCust = (customAnnotation *)annotation;
self.title=annotation.title;
self.description=tempCust.description;
self.phoneNumber=tempCust.phoneNumber;
//here is the question button
self.userInteractionEnabled=YES;
self.exclusiveTouch=NO;
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"Call" forState:UIControlStateNormal];
//decide the button's position
CGRect rect=button.frame;
rect.origin.x=15;
rect.origin.y=47;
rect.size.width=40;
rect.size.height=20;
button.frame=rect;
[self addSubview:button];
[button becomeFirstResponder];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
NSLog(@"initWithAnnotation finished");
return self;
}
//action method
-(void)buttonClicked:(id)sender{
NSString *callNumber=[[[NSString alloc] initWithFormat:@"tel://%@",self.phoneNumber] autorelease];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:callNumber]];
}
-(id)initWithAnnotation:(id)注释重用标识符:(NSString*)重用标识符
{
NSLog(@“initWithAnnotation开始”);
//初始化视图
self=[super initWithAnnotation:annotation-reuseIdentifier:reuseIdentifier];
if(self!=nil)
{
CGRect frame=self.frame;
frame.size=CGSizeMake(60.0,85.0);
self.frame=frame;
self.backgroundColor=[UIColor clearColor];
self.centerOffset=CGPointMake(30.0,42.0);
}
//这与我的问题无关
self.title=[[NSString alloc]init]autorelease];
self.description=[[NSString alloc]init]autorelease];
self.phoneNumber=[[NSString alloc]init]autorelease];
customAnnotation*tempCust=(customAnnotation*)注释;
self.title=annotation.title;
self.description=tempCust.description;
self.phoneNumber=tempCust.phoneNumber;
//这是问题按钮
self.userInteractionEnabled=YES;
self.exclusiveTouch=否;
UIButton*button=[UIButton button类型:UIButtonyPeroundRect];
[按钮设置标题:@“调用”状态:uicontrol状态正常];
//确定按钮的位置
CGRect=button.frame;
矩形原点x=15;
矩形原点y=47;
矩形尺寸宽度=40;
矩形尺寸高度=20;
button.frame=rect;
[自添加子视图:按钮];
[按钮成为第一响应者];
[按钮添加目标:自我操作:@选择器(按钮点击:)用于控制事件:UIControlEventTouchUpInside];
NSLog(@“initWithAnnotation finished”);
回归自我;
}
//作用方法
-(无效)按钮勾选:(id)发件人{
NSString*callNumber=[[NSString alloc]initWithFormat:@“tel://%@”,self.phoneNumber]autorelease];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:callNumber]];
}
为什么按钮不起作用
self.rightCalloutAccessoryView = button;
使用上述内容,而不是[自添加子视图:按钮]代码>
希望此帮助是您的谢谢,按钮可以高亮显示,按钮单击:methd可以触发。但是模拟器中没有呼叫。我现在知道了。这是因为模拟器不支持呼叫。对不起,我不明白您指的是什么。