UIButton - 在子视图中无法单击

时间:2015-05-25 12:08:44

标签: objective-c uibutton

我有一个包含2个按钮的SubView,当我点击按钮时,它不会调用选择器方法,我想知道我哪里出错了,这里是我的.h文件的内容

#import <UIKit/UIKit.h>

@interface FindingsView : UIView
{
    UIImageView *findingScreenView;
    UIView *backgroundTopView;
    UIView *backgroundLeftView;
    UIView *backgroundBottomView;
    UIView *backgroundRightView;
}

- (void) animationIn;
- (void) animationOut;
- (UIButton *)createButtonWithImageNamed:(NSString *)imageName andCGRect:(CGRect)Rect;

@end

以下是我的.m文件的内容

#import "FindingsView.h"

@implementation FindingsView

    - (id) init
    {
        self = [super init];
        [self setFrame:CGRectMake(0, 0, 1024, 768)];
        self.backgroundColor = [UIColor clearColor];

        findingScreenView = [[UIImageView alloc] init];
        [findingScreenView setImage:[UIImage imageNamed:@"new-finding-screen"]];
        [findingScreenView setFrame:CGRectMake(1024, 0, 1024, 768)];
        [self addSubview:findingScreenView];

        UIView *titleBar = [[UIView alloc] initWithFrame:CGRectMake(90, 250, 840, 35)];
        [titleBar setBackgroundColor:[UIColor colorWithRed:0.969  green:0.973  blue:0.976 alpha:1]];
        [titleBar.layer setCornerRadius:3];
        [findingScreenView addSubview:titleBar];

        CALayer *bottomBorder = [CALayer layer];
        bottomBorder.frame = CGRectMake(0.0f, 35.0f, titleBar.frame.size.width, 1.0f);
        bottomBorder.backgroundColor = [UIColor colorWithWhite:0.8f alpha:1.0f].CGColor;
        [titleBar.layer addSublayer:bottomBorder];

        UIButton *newPictureBtn = [self createButtonWithImageNamed:@"btn-newpic.png" andCGRect:CGRectMake(717, 185, 124, 35)];
        [newPictureBtn addTarget:self action:@selector(onNewPictureBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [findingScreenView addSubview:newPictureBtn];

        UIButton *closeBtn = [self createButtonWithImageNamed:@"btn-close-finding" andCGRect:CGRectMake(827, 185, 124, 35)];
        [closeBtn addTarget:self action:@selector(onCloseBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [findingScreenView addSubview:closeBtn];

    }

    -(void) animationIn
    {
        [UIView animateWithDuration:0.8 animations:^{
            self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
            findingScreenView.frame = CGRectMake(0, 0, 1024, 768);
        }];
    }

    -(void) animationOut
    {
        [UIView animateWithDuration:0.8 animations:^{
            findingScreenView.frame = CGRectMake(1024, 0, 1024, 768);
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }

    -  (UIButton *)createButtonWithImageNamed:(NSString *)imageName andCGRect:(CGRect)Rect
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        [btn setFrame:Rect];
        [btn setUserInteractionEnabled:YES];
        [btn setEnabled:YES];
        return btn;
    }

    - (IBAction)onCloseBtnClicked:(id)sender
    {
        NSLog(@"Close button clicked");
    }

    - (IBAction)onNewPictureBtnClicked:(id)sender
    {
        NSLog(@"Picture button clicked");
    }
@end

PS:为了澄清,我最初将其设置为显示出来,然后调用animationIn方法来设置动画并在视图中显示它。

谢谢。

0 个答案:

没有答案