由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [UIViewControllerWrapperView buttonPressed]:

问题描述:

我一直在尝试搜索解决方案,但类似问题的答案并没有完全指向正确的方向。问题如下由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [UIViewControllerWrapperView buttonPressed]:

在我的iOS应用程序中,我创建了一个自定义图像视图顶部的我的导航控制器。这是通过从指定视图控制器的alloc初始化BannerView类来完成的

例如,在我MasterViewController的viewDidLoad中我叫

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]; 


} 

现在,只要点击按钮时,我得到EXC_BAD_ACCESS要么没有控制台输出,或 终止应用程序由于未捕获的异常“NSInvalidArgumentException” ,原因:' - [UIViewControllerWrapperView buttonPressed]:无法识别的选择器发送到实例

任何人都可以帮我吗?我可能是在这里做一些错误的拓扑/继承,但我无法找到一个简单的方法来发现这一点..

预先感谢您

不知道是什么导致你的问题,但你确实有一些奇怪的构造在这里。

首先,通常不会将按钮添加到UIImageViews。相反,将它们添加到通用视图,然后调用通用视图的-bringSubviewToFront:以将按钮覆盖到图像视图上。

其次,选择器将发件人作为参数,所以您的选择应该是buttonPressed:并且方法应该是-(void)buttonPressed:(id)sender;