2012-12-14 39 views
3

我在註釋視圖中添加了一個按鈕,並且顯示正確。當輕按按鈕時,我想要撥打電話,但是當我點擊按鈕時,它不響應(按鈕甚至不亮)。看起來按鈕沒有收到觸摸事件,但我不知道爲什麼。作爲MKAnnotationView中子視圖的UIButton不起作用

這是我在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]]; 
} 

代碼爲什麼沒有按鈕的工作?

+0

對不起,我不知道你指的是什麼。 –

回答

1
self.rightCalloutAccessoryView = button; 

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

希望這有助於的你

+0

謝謝,該按鈕可以突出顯示,buttonClicked:可以觸發methd,但模擬器中不會調用。 –

+0

我現在知道了,這是因爲模擬器不支持通話。 –

+0

所以,對你來說很好。 – MadhuP

相關問題