【问题标题】:UIButton not clickable when custom view called调用自定义视图时 UIButton 不可点击
【发布时间】:2013-11-12 11:27:25
【问题描述】:

我是 iPhone 开发的新手,我需要帮助理解以下内容,因为我可以使用以下内容创建 newView

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]; 

上面的代码运行没有任何问题。但是当我尝试使用以下创建视图时,视图创建正常,但视图上的按钮不可点击。

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];

如果更改以下代码还有另一种情况

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

下面的代码导致应用崩溃

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

任何帮助将不胜感激,在此先感谢。

我已经添加了以下代码

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

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

它在第一种情况下工作:

我主要关心的是当 newView 被绘制时,为什么这个视图上的按钮不可点击

【问题讨论】:

  • 您是否已将按钮添加到视图中? [newView addSubview:newViewBtn];
  • @GameDevGuru :是的,我添加了按钮
  • @Aiden 你试过我的代码了吗..我很确定它会帮助你。
  • 我主要关心的是当 newView 被绘制时,为什么这个视图上的按钮不可点击
  • @Aiden 试试我的回答,看看点击按钮时是否收到响应。

标签: ios uiviewcontroller uibutton user-interaction


【解决方案1】:

向 UIButton 添加自定义子视图时,您必须关闭 userInteractionEnabled 属性。

customView.userInteractionEnabled = NO;

然后添加到您的按钮。

[customButton addSubView:customView];

否则,您的自定义视图会在按钮之前拦截触摸事件,因为它已添加到视图堆栈的顶部。

【讨论】:

    【解决方案2】:

    你需要像这样添加目标...

       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");
       }
    

    【讨论】:

      【解决方案3】:

      这里的问题是下面的那行,因为新的子视图必须根据父视图

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

      将上面写成如下解决了问题

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

      【讨论】:

        【解决方案4】:

        我相信你忘了添加一个目标(点击按钮时调用的函数):

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

        确保您定位的函数存在,否则点击时会崩溃。

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

        【讨论】:

          【解决方案5】:

          试试下面的代码...

          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");
          }
          

          【讨论】:

            【解决方案6】:

            你忘了这个:

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

            请试试这个。

            【讨论】:

            • 我也添加了这个 [newViewBtn addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside]; }
            • 你用这个方法登录了吗?
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多