Iphone 在UIView中添加按钮

Iphone 在UIView中添加按钮,iphone,cocoa-touch,cocoa,Iphone,Cocoa Touch,Cocoa,我试图在UIview中添加一个按钮,以覆盖视图的整个区域。。像这样: -(MBNHomeScreenButton*) initWithImage:(UIImage*)image andHighlightedImage:(UIImage*)highlightedImage andTitle:(NSString*)aTitle withSelector:(SEL)action

我试图在UIview中添加一个按钮,以覆盖视图的整个区域。。像这样:

-(MBNHomeScreenButton*) initWithImage:(UIImage*)image 
              andHighlightedImage:(UIImage*)highlightedImage 
                         andTitle:(NSString*)aTitle 
                     withSelector:(SEL)actionButton
                      forDelegate:(UIViewController*)viewController{
self = [super init];
if (self) {
    self.frame = CGRectMake(0, 0, 100, 120);

    self.imageView = [[UIImageView alloc] initWithImage:image highlightedImage:highlightedImage];
    self.imageView.center = self.center;
    self.imageView.frame = CGRectMake(10, 0, HS_BUTTON_IMAGE_WIDTH, HS_BUTTON_IMAGE_HEIGHT);
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.imageView.highlighted = NO;
    [self addSubview:self.imageView];

    self.title = [[UILabel alloc] initWithFrame:CGRectMake(0, HS_BUTTON_IMAGE_HEIGHT, HS_BUTTON_IMAGE_WIDTH + 20, 30)];
    self.title.text = aTitle;
    self.title.textAlignment = UITextAlignmentCenter;
    self.title.textColor = [UIColor whiteColor];
    self.title.backgroundColor = [UIColor clearColor];

    [self addSubview:self.title];

    // This creates a transparent button over the view
    UIButton *button  = [UIButton buttonWithType:UIButtonTypeCustom];
    //button.backgroundColor = [UIColor redColor];
    [button setFrame:self.frame];
    [button addTarget:viewController action:actionButton forControlEvents:UIControlEventTouchUpInside];
    [self addSubview: button];
}
return self;}
m_newsHSButton = [[MBNHomeScreenButton alloc] initWithImage:[UIImage imageNamed:NEWS_SHADED_IMAGE]
                                        andHighlightedImage:[UIImage imageNamed:NEWS_HIGHLIGHTED_IMAGE]
                                                   andTitle:@"News" 
                                               withSelector:@selector(newsButtonPressed)
                                                forDelegate:self];

m_newsHSButton.center = CGPointMake(m_backgroundView.frame.size.width / 4, m_backgroundView.frame.size.height / 4);
[backgroundImgView addSubview:m_newsHSButton];
在ViewController中,我创建如下视图:

-(MBNHomeScreenButton*) initWithImage:(UIImage*)image 
              andHighlightedImage:(UIImage*)highlightedImage 
                         andTitle:(NSString*)aTitle 
                     withSelector:(SEL)actionButton
                      forDelegate:(UIViewController*)viewController{
self = [super init];
if (self) {
    self.frame = CGRectMake(0, 0, 100, 120);

    self.imageView = [[UIImageView alloc] initWithImage:image highlightedImage:highlightedImage];
    self.imageView.center = self.center;
    self.imageView.frame = CGRectMake(10, 0, HS_BUTTON_IMAGE_WIDTH, HS_BUTTON_IMAGE_HEIGHT);
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.imageView.highlighted = NO;
    [self addSubview:self.imageView];

    self.title = [[UILabel alloc] initWithFrame:CGRectMake(0, HS_BUTTON_IMAGE_HEIGHT, HS_BUTTON_IMAGE_WIDTH + 20, 30)];
    self.title.text = aTitle;
    self.title.textAlignment = UITextAlignmentCenter;
    self.title.textColor = [UIColor whiteColor];
    self.title.backgroundColor = [UIColor clearColor];

    [self addSubview:self.title];

    // This creates a transparent button over the view
    UIButton *button  = [UIButton buttonWithType:UIButtonTypeCustom];
    //button.backgroundColor = [UIColor redColor];
    [button setFrame:self.frame];
    [button addTarget:viewController action:actionButton forControlEvents:UIControlEventTouchUpInside];
    [self addSubview: button];
}
return self;}
m_newsHSButton = [[MBNHomeScreenButton alloc] initWithImage:[UIImage imageNamed:NEWS_SHADED_IMAGE]
                                        andHighlightedImage:[UIImage imageNamed:NEWS_HIGHLIGHTED_IMAGE]
                                                   andTitle:@"News" 
                                               withSelector:@selector(newsButtonPressed)
                                                forDelegate:self];

m_newsHSButton.center = CGPointMake(m_backgroundView.frame.size.width / 4, m_backgroundView.frame.size.height / 4);
[backgroundImgView addSubview:m_newsHSButton];
还是。。。按下的新闻按钮不会被呼叫

你能帮忙吗? 谢谢

==================== [编辑] 好啊我想出来了。。谢谢各位。 事实上,我试图将视图作为子视图添加到UIImageView,最后一行是:

[backgroundImgView addSubview:m_newsHSButton];
很明显,它再也不会被触碰了


谢谢大家的努力

尝试在自定义视图中将按钮设置为@property或instance变量。然后调用
[m_newshButton.button addTarget:self action:actionButton for ControlEvents:UIControlEventTouchUpInside]来自viewController

你能把你的方法显示出来吗?我刚刚用简单的ViewController项目尝试了你的代码,它运行得很好。你能确认它有正确的签名吗:-(作废)新闻按钮按下。有很多可能性,请检查。此外,我似乎还记得任何透明的东西(当您执行
addTarget
时,
viewController
的值是多少?您是否需要使用
self.viewController
?好的……所以是的……选择器很好:-(void)newsButtonPressed{NSLog(@“newsButtonPressed”);)