简体   繁体   中英

UIButton added to UIButton not responding to selector

I have added an ui button to a disclouser button

And implemented selector method

but the action selector doesn't respond to the button why and what to do

//To add a discloser button to show route map
UIButton *detailBtn=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.leftCalloutAccessoryView=detailBtn;
[detailBtn addTarget:self action:@selector(showRoute:) forControlEvents:UIControlEventTouchUpInside];
DisplayMap *ann=(DisplayMap *)annotation;
detailBtn.tag = ann.detailButtonTag;


UIButton *btnOnPopup=[[UIButton alloc] initWithFrame:CGRectMake(- 207, -6, 300, 43)];
btnOnPopup.backgroundColor = [UIColor whiteColor];
[btnOnPopup addTarget:self action:@selector(showRoute2:) forControlEvents:UIControlEventTouchUpInside];
btnOnPopup.tag = ann.detailButtonTag;
btnOnPopup.userInteractionEnabled = YES;
[detailBtn insertSubview:btnOnPopup aboveSubview:detailBtn];

the selector

[btnOnPopup addTarget:self action:@selector(showRoute2:) forControlEvents:UIControlEventTouchUpInside];

(Second button btnOnPopup) is not working.

Your btnOnPopup is out of detailBtn 's bounds so touches won't even get to it. Adding a button to a button is not really a great practice anyway. Adding it to a button's superview might be a little bit better (and will make your touches work, but you might need to adjust frame)

[detailBtn.superview insertSubview:btnOnPopup aboveSubview:detailBtn];

Add view and tap gesture recognizer instead of button:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHandler:)];
[singleTap setNumberOfTapsRequired:1];
[view addGestureRecognizer:singleTap];

...

[detailBtn addSubview:view];

...

- (void)tapGestureHandler:(UIGestureRecognizer *)gestureRecognizer
{
    // your code for tap handler is here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM