开发者

iphone UIButton selector not working when frame origin is not 0,0

I've tried many many different ways but can't find what's wrong with my code, hope you can help me!

I'm making a custom callout bubble to be shown when clicking on a pin on a map, to show some images inside the bubble, and I want the detaildisclosure button so the user can click and access to more info. My problem is that the bubble and button are shown but the click on the button does not called the selector method.

//EDIT: sorry because the first time I put too much code, I'll edit trying to be more specific, thanks for your answers

In the method:

- (id) initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier

At first I show an imageview to change a custom icon instead of normal pin

self = [super initWithAnnotation:myAnnotation reuseIdentifier:reuseIdentifier];
self.frame = CGRectMake(0, 0, kWidth, kHeight);
self.backgroundColor = [UIColor clearColor];

imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon.png"]];
imageView.frame = CGRectMake(kBorder, kBorder, kWidth - 2 * kBorder, kWidth - 2 * kBorder);
[self addSubview:imageView];

I add it as a subview, and at the same time to customize the bubble callout I create some labels and images and I added to a view calloutView that it's hidden at first till the select method of the pin is called

   calloutView = [[UIView alloc] initWithFrame:CGRectMake(10, 30, restNameLbl.frame.size.width + 30, 60)];
   calloutView.hidden = YES;

After that I create rounded rect background and size to text, I leave out that part of code because I don't think it's important now, let me know if I should put it, I use some graphics functions to simulate the default iphone bubble map sytle

And I add all the elements created before to the calloutView:

[calloutView addSubview:backgroundRect];         
[calloutView addSubview:restNameLbl];        
[calloutView addSubview:likeImg];
[calloutView addSubview:likesLbl]; 
[calloutView addSubview:noLikeImg];
[calloutView addSubview:noLikesLbl];

And then I place that view at the right place

//position callout above pin
calloutView.frame = CGRectMake(-25, -calloutView.frame.size.height-10, calloutView.frame.size.width + 50, calloutView.frame.size.height);       
[self addSubview:calloutView];

And in the same bubble view - called calloutView I want to show a detail disclosure button so that the user can access to more info, but if I added it directly to the calloutView the selector events are never trigger. So I tried to do it in a different way that it's adding a button to the parent view, put its hidden state to yes and show it on the selector method

accessory = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[accessory setTitle:myAnnotation.idRest forState:UICont开发者_Python百科rolStateNormal];
accessory.hidden= YES;  

//NOTE: here it's the problem if i leave out this frame line, everything works perfect or if i set the origin.x and origin.y to 0,0 but when i changed that number to show it at its right place of the view the button selector stops working

accessory.frame = CGRectMake(110, calloutView.frame.origin.y+25, accessory.frame.size.width, accessory.frame.size.height);

Then I add the selector event to called when click and as I said the function calloutAccessoryTap it's called only if the frame of accessory it's not indicated or if origin.x and y are both set to 0

accessory.userInteractionEnabled = YES;
[accessory addTarget:self action:@selector(calloutAccessoryTap) forControlEvents: UIControlEventTouchUpInside | UIControlEventTouchCancel];

And last I added the button to the main view

[self addSubview:accessory];

So at first the view is just an icon, the callout view with the labels and images is hidden, the same happen to the detail button and when the pin is selected and the bubble needs to be shown the callout view and the button are shown. Everything is seen right on the screen, but when the frame of the accessory is set to the good position - distinct to origin x and y 0,0 the selector stop working.

Hope you can help me, because I tried many different things but none of them are working.


It's time consuming to go through the code so I'll just give you a pointer. Are you adding subViews on top of the UIButton "accessory" ? If yes, then of course the button won't detect taps. subViews are added on top of each other so putting a transparent view on top of the button will effectively render it useless.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜