UIButton not clickable when custom view called

落花浮王杯 提交于 2019-12-14 03:48:00

问题


I am new to iPhone development,I need help in understanding the below,as I am able to create newView using the following

UIView *newView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 80, 30)]; 
newView.backgroundColor=[UIColor clearColor];
UIButton *newViewBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];


newViewBtn.frame = CGRectMake(newView.frame.origin.x+5,
                            newView.frame.origin.y+5,60,20);
[newView addSubview:newViewBtn];
[self.view addSubview:newView]; 

Above code is working without any problem. But when I try to create the view using the following, view is created alright, but the button on the view is not clickable.

int randNumX = arc4random() % 150;
int randNumY = arc4random() % 200;
UIView newView=[[UIView alloc]init];
newView.frame =CGRectMake(randNumX, randNumY, 80, 30);

newView.backgroundColor=[UIColor clearColor];

UIButton *newViewBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
newViewBtn.frame  = CGRectMake(newView.frame.origin.x+5
                         ,newView.frame.origin.y+5,60,20);
[newView addSubview:newViewBtn];
[self.view addSubview:newView];

and there is one another scenario if change the following code

 newViewBtn.frame = CGRectMake(newView.frame.origin.x+5
                          ,newView.frame.origin.y+5,60,20);

with the code below app crashes

 newViewBtn.frame =CGRectMake(randNumX+5,randNumY+5,60,20);

Any help will be greatly appreciated,thanks in advance.

I have added the following code already

     newViewBtn addTarget:self action:@selector(btnclick:)forControlEvents:UIControlEventTouchUpInside];

     -(void)btnclick:(id)sender
     {
       //my code
     }

It is working in the first case:

My main concern here is when the newView is draw ,why the button on this view is not clickable


回答1:


When adding a custom subview to a UIButton you have to run off the userInteractionEnabled property.

customView.userInteractionEnabled = NO;

then add to your button.

[customButton addSubView:customView];

Otherwise your custom view intercepts touch events before the button since it's added to the top of the view stack.




回答2:


Here the problem is the Line below ,As the new child view must be according to the parent view

    newViewBtn.frame =CGRectMake(randNumX+5,randNumY+5,60,20);

writting the above as below solved the problem

    newViewBtn.frame =CGRectMake(5,5,60,20);



回答3:


You forgot this :

 UIButton *newViewBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
  [newViewBtn addTarget:self action:@selector(clickeMe) forControlEvents:UIControlEventTouchUpInside];

Please try this.




回答4:


Try following code...

int randNumX = arc4random() % 150;
int randNumY = arc4random() % 200;

UIView newView=[[UIView alloc]init];

newView.frame =CGRectMake(randNumX, randNumY, 80, 30);

newView.backgroundColor=[UIColor clearColor];

UIButton *newViewBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

newViewBtn.frame  = CGRectMake(0,0,60,20);

newViewBtn.center = newView.center;

[newViewBtn addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];

[newView addSubview:newViewBtn];

[self.view addSubview:newView];


-(void)btnTapped:(id)sender
{
     NSLog(@"btnTapped");
}



回答5:


I believe you forgot to add a target (the function called when the button is clicked):

 [newViewBtn addTarget:self action:@selector(yourfunction:) forControlEvents:UIControlEventTouchUpInside];

Make sure that the function you're targeting exists, or it will crash when you click.

- (void) yourfunction {
    NSLog(@"Click Detected");
}



回答6:


You need to add target like this...

   UIButton *newViewBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
   newViewBtn.frame=CGRectMake(newView.frame.origin.x+5,newView.frame.origin.y+5,60,20);
   [newViewBtn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside]; // ---- add this line.....
   [newView addSubview:newViewBtn];

   -(void)btnPressed:(id)sender
   {
        NSLog(@"Btn Pressed");
   }


来源:https://stackoverflow.com/questions/19723772/uibutton-not-clickable-when-custom-view-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!