gpt4 book ai didi

objective-c - UIButton 作为 MKAnnotationView 中的 subview 不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:23:22 25 4
gpt4 key购买 nike

我在注释 View 中添加了一个按钮,它显示正确。点击按钮时,我想调用电话,但是当我点击按钮时,它没有响应(按钮甚至没有突出显示)。按钮似乎没有收到触摸事件,但我不知道为什么。

这是我在 customAnnotationView.m 中的代码

- (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]];
}

为什么按钮不起作用?

最佳答案

self.rightCalloutAccessoryView = button;

使用上面的代替[self addSubview:button];

希望对你有帮助

关于objective-c - UIButton 作为 MKAnnotationView 中的 subview 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13872460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com