Search code examples
iosobjective-cuiviewcontrolleruibuttonaddtarget

Call method in UIViewController to target error


in my MainMainViewController I created new UIView (ContentView). I set

DetailViewController *detailView = 
   [[DetailViewController alloc] initWithObject:paAnnotation];
detailView.view.frame = CGRectMake(0, 
                                   0, 
                                   [self getWidthOfScreen], 
                                   [self getWidthOfScreen]);
[contentView addSubview:detailView.view];

It works fine but in my DetailViewController, I created UIButton and set target action. But when I click on button it crashes and show me error. Can you tell me where can be the problem?

- (id)initWithObject:paAnnotation {

    if ((self = [super init])) {
        self.annotation = paAnnotation;
    }

    return self;
}    

- (void)viewDidAppear:(BOOL)animated {

        UIButton *closeButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
        closeButton.backgroundColor = [UIColor redColor];
        [closeButton addTarget:self 
                        action:@selector(closeContent) 
              forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:closeButton];

}

- (void)closeContent {
    NSLog(@"Test");
}

ERROR:

-[GEOVectorTile closeContent]: unrecognized selector sent to instance 0x19d0a1e0

Solution

  • In your MainMainViewController, try to retain your detailView when you initialise it. Or create detailView as retain property in MainMainViewController and then use it.