0
votes

I've been trying to search for solution, but answers to similar problems didn't quite point me in right direction. The problem is as follows

in my iOS app i create a custom image view atop of my navigation controller. this is done via alloc initing BannerView Class from designated view controllers

e.g. in viewDidLoad of my MasterViewController i call

BannerView *bannerLogoView = [[BannerView alloc]init];
//add the tabcotroller as a subview of the view
[self.tabBarController.view addSubview:bannerLogoView.bannerView];

BannerView.m

#import "BannerView.h"

@interface BannerView ()

@end


@implementation BannerView


-(id)init {

self = [super init];

if (self) {

    self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bannerImage.jpg"]];
    self.bannerView = [[UIView alloc] initWithFrame:CGRectMake(0,20, 320, 30)];
    [self.bannerView addSubview:self.imageView];
    //NSLog(@"Setting up image from within banner view");
    [self setupButton];



}
return self;

}

- (void)buttonPressed {
NSLog(@"Button pressed");
}


-(void) setupButton {
self.imageView.userInteractionEnabled = YES;    
self.button= [UIButton buttonWithType:UIButtonTypeRoundedRect];

[self.button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];

self.button.frame = CGRectMake(0.0, 0.0, 320.0, 30.0);
[self.imageView addSubview:self.button];


}

Now anytime the button is clicked i get either EXC_BAD_ACCESS with no console output, or Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewControllerWrapperView buttonPressed]: unrecognized selector sent to instance

Could anyone help me out? I might be doing something wrong with topology/inheritance here, but I can't find a simple way to find this out..

Thank you in advance

1

1 Answers

1
votes

Not sure what's causing your problem but you do have some weird constructs here.

First, usually buttons are not added to UIImageViews. Instead, add them both to a common view and then call the common view's -bringSubviewToFront: to overlay the button onto the image view.

Second, the selector takes the sender as an argument, so your selection should be buttonPressed: and method should be -(void)buttonPressed:(id)sender;