IBAction is not getting called for Button inside UIView

ios, objective-c, uiview

Solution

Try using the following static method to get TestView

+ (TestView *)getNewTestView {
    TestView *view = nil;
    NSArray *xib = [[NSBundle mainBundle] loadNibNamed:@"TestView" owner:nil options:nil];
    for (NSObject *obj in xib) {
        if ([obj isKindOfClass:[TestView class]]) {
            view = (TestView *)obj;
        }
    }
    return view;

}

And then, to get an instance of the object, use:

 TestView *testView = [TestView getNewTestView];
 [self.view addSubview:testView];

Problem

I have created a view for custom callout with a button. This custom callout gets displayed on click of any annotation on Mapview. But nothing happens when I click the button inside the custom callout. ``` #import <UIKit/UIKit.h> @interface TestView : UIView @end #import "TestView.h" @implementation TestView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } -(IBAction)showMessage:(id)sender { NSLog(@"Test"); } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { if(![view.annotation isKindOfClass:[MKUserLocation class]]) { CGRect rect = CGRectMake(0,0,268,140); TestView *testview = [[TestView alloc] initWithFrame:rect]; testview = [self loadNibNamed:@"TestView" ofClass:[TestView class]]; [view addSubview:testview]; } } ``` The view is loaded using loadNibNamed method. ``` - (id)loadNibNamed:(NSString *)nibName ofClass:(Class)objClass { if (nibName && objClass) { NSArray *objects = [[NSBundle mainBundle] loadNibNamed:nibName owner:nil options:nil]; for (id currentObject in objects ){ if ([currentObject isKindOfClass:objClass]) return currentObject; } } return nil; } ``` I have checked the userInteractionEnabled property for View and Button and both are set to true. Also mapped the showMessage with Touch Up Inside event of button. any suggestion on how I go about identifying this problem?

Original source