Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/122.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 Can';t单击UIView中的按钮_Ios_Objective C_Uiview - Fatal编程技术网

Ios Can';t单击UIView中的按钮

Ios Can';t单击UIView中的按钮,ios,objective-c,uiview,Ios,Objective C,Uiview,我在self.view中创建了一个menuView,menuView有两个子视图,它们是UIView。 此UIView包含4个UIButton,但我无法单击其中任何一个userInteractionEnabledset YES,按钮的框架在视图调试中设置正确。 代码: 更新: ViewController.m: - (void)viewDidLoad { [super viewDidLoad]; LeftMenuView *leftView=[LeftMenuView creat

我在
self.view
中创建了一个menuView,menuView有两个子视图,它们是UIView。 此UIView包含4个UIButton,但我无法单击其中任何一个
userInteractionEnabled
set YES,按钮的框架在视图调试中设置正确。 代码:

更新:

ViewController.m:

- (void)viewDidLoad {
    [super viewDidLoad];
    LeftMenuView *leftView=[LeftMenuView createLeftMenuView];
    leftView.userInteractionEnabled=YES;
    self.leftView=leftView;
    [self.view addSubview:leftView];
    //nightButton
    UIButton *nightButton=[self.leftView viewWithTag:6];
    [nightButton addTarget:self action:@selector(touchButton:) forControlEvents:UIControlEventTouchUpInside];
    leftView.userInteractionEnabled=YES;
    //set
    UIButton *settingButton=[self.leftView viewWithTag:4];
    [settingButton addTarget:self.leftView action:@selector(push) forControlEvents:UIControlEventTouchUpInside];
    }
**LeftMenuView.m:*这是一个UIView类。这是我设置按钮和菜单的地方

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    self.backgroundColor=[UIColor colorWithRed:26/256.f green:31/256.f blue:36/256.f alpha:0.7];
    [self addHeaderView];
    [self addButton];
    return self;
}
#pragma mark -add view to menu view
-(void)addHeaderView
{
    UIView *headViewUp=[[UIView alloc]init];
    headViewUp.userInteractionEnabled=YES;
    headViewUp.frame=CGRectMake(0, 0, yScreenWidth/2, 114);
    headViewUp.backgroundColor=ColorWithRGB(26, 31, 36);
    headViewUp.backgroundColor=[UIColor redColor];
    self.headViewUp=headViewUp;
    [self addSubview:headViewUp];
    UIView *headViewDown=[[UIView alloc]init];
    headViewDown.userInteractionEnabled=YES;
    headViewDown.frame=CGRectMake(0, yScreenHeight-50, yScreenWidth/2, 50);
    headViewDown.backgroundColor=ColorWithRGB(26, 31, 36);
    self.headViewDown=headViewDown;
    [self addSubview:headViewDown];
}
-(void)addButton
{
    UIButton *settingButton=[self setFrame:CGRectMake(yScreenWidth*2/6, 64, yScreenWidth/6, 50 ) title:@"设置" image:@"Menu_Icon_Setting"];
    settingButton.userInteractionEnabled=YES;
    settingButton.tag=4;
    [self resizeTextAndImageInButton:settingButton];
    [self.headViewUp addSubview:settingButton];
    UIButton *nightButton=[self setFrame:CGRectMake(yScreenWidth/4, 0, yScreenWidth/4, 50 ) title:@"白天" image:@"Menu_Day"];
    nightButton.tag=6;
    [self.headViewDown addSubview:nightButton];
}
-(UIButton *)setFrame:(CGRect)frame title:(NSString *)title image:(NSString *)imageName
{
    UIButton *button=[[UIButton alloc]initWithFrame:frame];
    [button setTitle:title forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    button.titleLabel.font=[UIFont systemFontOfSize:8];
    button.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
    return button;
}
-(void)resizeTextAndImageInButton:(UIButton *)button
{
    [button setTitleEdgeInsets:UIEdgeInsetsMake(button.imageView.frame.size.height ,-button.imageView.frame.size.width, 0.0,0.0)];
    [button setImageEdgeInsets:UIEdgeInsetsMake(-10, 0.0,0.0, -button.titleLabel.bounds.size.width)];
}

clilk按钮的方法不起作用。

您需要使用相应的方法为按钮添加目标

[button addTarget:self 
           action:@selector(aButtonMethod:)
 forControlEvents:UIControlEventTouchUpInside];
此代码需要以下内容

-(void)addButton
{
    UIButton *settingButton=[self setFrame:CGRectMake(yScreenWidth*2/6, 64, yScreenWidth/6, 50 ) title:@"设置" image:@"Menu_Icon_Setting"];
    settingButton.userInteractionEnabled=YES;
    settingButton.tag=4;
    [self resizeTextAndImageInButton:settingButton];
    [self.headViewUp addSubview:settingButton];
    UIButton *nightButton=[self setFrame:CGRectMake(yScreenWidth/4, 0, yScreenWidth/4, 50 ) title:@"白天" image:@"Menu_Day"];
    nightButton.tag=6;
    **[nightButton addTarget:self 
               action:@selector(aButtonMethod:)
     forControlEvents:UIControlEventTouchUpInside];**
    [self.headViewDown addSubview:nightButton];
}


基本上创建同一按钮两次,但仅为一组添加目标,而为另一组不将其添加到视图中

您需要使用相应的方法为按钮添加目标

[button addTarget:self 
           action:@selector(aButtonMethod:)
 forControlEvents:UIControlEventTouchUpInside];
此代码需要以下内容

-(void)addButton
{
    UIButton *settingButton=[self setFrame:CGRectMake(yScreenWidth*2/6, 64, yScreenWidth/6, 50 ) title:@"设置" image:@"Menu_Icon_Setting"];
    settingButton.userInteractionEnabled=YES;
    settingButton.tag=4;
    [self resizeTextAndImageInButton:settingButton];
    [self.headViewUp addSubview:settingButton];
    UIButton *nightButton=[self setFrame:CGRectMake(yScreenWidth/4, 0, yScreenWidth/4, 50 ) title:@"白天" image:@"Menu_Day"];
    nightButton.tag=6;
    **[nightButton addTarget:self 
               action:@selector(aButtonMethod:)
     forControlEvents:UIControlEventTouchUpInside];**
    [self.headViewDown addSubview:nightButton];
}


基本上,您创建了两次相同的按钮,但仅为一组添加目标,而对于另一组,您不将它们添加到视图中

我查看了您的代码。您犯了一个错误,没有为“LefMenuView”设置框架。即使您没有这样做,其子视图仍然可见,但不可触摸或单击

所以试试这个。我通过添加下面的代码来运行。现在我可以看到点击操作了

Rule of thumb : if super view frame is Zero then all subviews are visible but not touchable.(Because you are touching out side of bounds).
ViewController.m



我检查了你的密码。您犯了一个错误,没有为“LefMenuView”设置框架。即使您没有这样做,其子视图仍然可见,但不可触摸或单击

所以试试这个。我通过添加下面的代码来运行。现在我可以看到点击操作了

Rule of thumb : if super view frame is Zero then all subviews are visible but not touchable.(Because you are touching out side of bounds).
ViewController.m



您在代码中设置目标时犯了错误。 请这样做

[settingButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
设置
settingButton.userInteractionEnabled=YES

现在按如下方式处理click事件

-(void)buttonPressed:(id) sender
{

   UIButton *btn=(UIButton*) sender;


}

您在代码中设置目标时犯了错误。 请这样做

[settingButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
设置
settingButton.userInteractionEnabled=YES

现在按如下方式处理click事件

-(void)buttonPressed:(id) sender
{

   UIButton *btn=(UIButton*) sender;


}

你预计会发生什么?在
viewDidLoad
中,您从未将这些按钮添加到视图中,而在
addButton
中,您没有将目标添加到这些按钮中。请正确调试。@WMios我在LeftMenuView中添加此按钮,并在viewDidLoad中添加目标。此按钮可以显示但不能单击。您的视图在scrollview中吗?@SwiftyCruz,不,它只是一个UIView。LeftMenuView有两个子视图,也是视图。您希望发生什么?在
viewDidLoad
中,您从未将这些按钮添加到视图中,而在
addButton
中,您没有将目标添加到这些按钮中。请正确调试。@WMios我在LeftMenuView中添加此按钮,并在viewDidLoad中添加目标。此按钮可以显示但不能单击。您的视图在scrollview中吗?@SwiftyCruz,不,它只是一个UIView。LeftMenuView有两个子视图,也是视图。我将其添加到-(void)viewDidLoad中。使用viewDidLoad中的按钮标记,您不会在addbutton方法中将按钮添加到视图中。您不会添加目标
-(void)addbutton
在UIView类中。我是否应该在UIView类中添加click方法?我刚刚在viewDidLoad中添加了它,但它不能单击。我在Github中发布了项目。在你的ViewController中。m你创建了一个按钮,设置了一个选择器,然后再也不在视图中显示它,你需要将它添加到视图中,否则它将永远不会被使用。我将它添加到视图中,但它也不起作用。您的意思是UIButton*nightButton=[self.leftView viewWithTag:6];这是一个新按钮,而不是我用标记设置的按钮?我将其添加到-(void)viewDidLoad中。使用viewDidLoad中的按钮标记,您不会在addbutton方法中将按钮添加到视图中。您不会添加目标
-(void)addbutton
在UIView类中。我是否应该在UIView类中添加click方法?我刚刚在viewDidLoad中添加了它,但它不能单击。我在Github中发布了项目。在你的ViewController中。m你创建了一个按钮,设置了一个选择器,然后再也不在视图中显示它,你需要将它添加到视图中,否则它将永远不会被使用。我将它添加到视图中,但它也不起作用。您的意思是UIButton*nightButton=[self.leftView viewWithTag:6];这是一个新按钮,而不是我用标签设置的按钮?