Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS子视图,按钮不响应触摸。_Ios_Uiview_Subview - Fatal编程技术网

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

iOS子视图,按钮不响应触摸。,ios,uiview,subview,Ios,Uiview,Subview,我有一个UIView子类,它有一个带有按钮的框架/图像。我正试图将它添加到我的视图控制器中,但是当我添加时,它会显示出来,但没有一个按钮会显示任何触摸 这是我的UIView子类 #import "ControlView.h" @implementation ControlView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { CGR

我有一个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之间,我有一个心理障碍

你在这么做吗

 ControlView *cont = [[ControlView alloc]init]; 
你应该这样做

ControlView *cont = [[ControlView alloc]initWithFrame:self.view.frame];
你在这么做

 ControlView *cont = [[ControlView alloc]init]; 
你应该这样做

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

先生,您是位绅士和学者。很好用,我知道这是件小事/愚蠢的事。事实上,现在我正好相反。。它添加到的视图将不会显示与该代码的接触。先生,您是一位绅士和学者。很好用,我知道这是件小事/愚蠢的事。事实上,现在我正好相反。。它添加到的视图将不会注册与该代码的接触。