iOS子视图,按钮没有响应触摸。

时间:2012-03-01 02:13:24

标签: ios uiview subview

我有一个UIView子类,其中有一个带有按钮的帧/图像。我正在尝试将它添加到我的视图控制器中,但是当我这样做时会显示但没有任何按钮会记录任何触摸。

这是我的UIView子类。

    #import "ControlView.h"

@implementation ControlView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        CGRect slideFrame = CGRectMake(0, 402, 320, 82);
        slider = [[UIView alloc] initWithFrame:slideFrame];
        slider.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"shelf.png"]];
        [self addSubview:slider];

        UIImage *btnImage = [UIImage imageNamed:@"NavBar_01.png"];
        UIImage *btnImageSelected = [UIImage imageNamed:@"NavBar_01_s.png"];

        btnImage = [UIImage imageNamed:@"NavBar_01.png"];
        btnImageSelected = [UIImage imageNamed:@"NavBar_01_s.png"];
        btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn1.frame = CGRectMake(12, 28, 70, 50);
        [btn1 setBackgroundImage:btnImage forState:UIControlStateNormal];
        [btn1 setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; 
        [slider addSubview:btn1];
        [slider bringSubviewToFront:btn1];
        [btn1 addTarget:self action:@selector(home) forControlEvents:UIControlEventTouchUpInside];

        btnImage = [UIImage imageNamed:@"NavBar_02.png"];
        btnImageSelected = [UIImage imageNamed:@"NavBar_02_s.png"];
        btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn2.frame = CGRectMake(87, 28, 70, 50);
        [btn2 setBackgroundImage:btnImage forState:UIControlStateNormal];
        [btn2 setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; 
        [slider addSubview:btn2];
        [slider bringSubviewToFront:btn2];
        // [btn2 addTarget:self action:@selector() forControlEvents:UIControlEventTouchUpInside];

        btnImage = [UIImage imageNamed:@"NavBar_03.png"];
        btnImageSelected = [UIImage imageNamed:@"NavBar_03_s.png"];
        btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn3.frame = CGRectMake(162, 28, 70, 50);
        [btn3 setBackgroundImage:btnImage forState:UIControlStateNormal];
        [btn3 setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; 
        [slider addSubview:btn3];
        [slider bringSubviewToFront:btn3];
        // [btn3 addTarget:self action:@selector() forControlEvents:UIControlEventTouchUpInside];

        btnImage = [UIImage imageNamed:@"NavBar_04.png"];
        btnImageSelected = [UIImage imageNamed:@"NavBar_04_s.png"];
        btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn4.frame = CGRectMake(237, 28, 70, 50);
        [btn4 setBackgroundImage:btnImage forState:UIControlStateNormal];
        [btn4 setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; 
        [slider addSubview:btn4];
        [slider bringSubviewToFront:btn4];
        // [btn4 addTarget:self action:@selector() forControlEvents:UIControlEventTouchUpInside];

        UISwipeGestureRecognizer *recognizer;

        recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(slideUp)];
        [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
        [self addGestureRecognizer:recognizer];

        recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(slideDown)];
        [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
        [self addGestureRecognizer:recognizer];

    }
    return self;
}

-(void)slideUp
{
    // Slide up based on y axis
    CGRect frame = slider.frame;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.75];
    frame.origin.y = 320;
    slider.frame = frame;
    [UIView commitAnimations];  
}

-(void)slideDown
{
    CGRect frame = slider.frame;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.75];
    frame.origin.y = 425;
    slider.frame = frame;
    [UIView commitAnimations];  
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

这就是我将它添加到我的ViewController的方式。

 ControlView *cont = [[ControlView alloc]init];
    [homeView.view addSubview:cont];

我尝试将相同的代码添加到ViewController和按钮等工作,所以我确定这是简单/愚蠢的代理/自我等..在子视图和ViewController之间,但我有一个心理块。

1 个答案:

答案 0 :(得分:5)

你这样做了吗?

 ControlView *cont = [[ControlView alloc]init]; 

你应该这样做

ControlView *cont = [[ControlView alloc]initWithFrame:self.view.frame];