0

我一直在尝试寻找解决方案,但类似问题的答案并没有为我指明正确的方向。问题如下

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

例如,在我的 MasterViewController 的 viewDidLoad 中,我调用

BannerView *bannerLogoView = [[BannerView alloc]init];
//add the tabcotroller as a subview of the view
[self.tabBarController.view addSubview:bannerLogoView.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]:无法识别的选择器已发送到实例

谁能帮帮我?我可能在这里对拓扑/继承做错了,但我找不到一种简单的方法来解决这个问题..

先感谢您

4

1 回答 1

1

不知道是什么导致了你的问题,但你确实有一些奇怪的结构。

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

其次,选择器将发送者作为参数,因此您的选择应该是 buttonPressed: 并且方法应该是-(void)buttonPressed:(id)sender;

于 2013-09-23T01:18:46.370 回答