6

我是 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 被绘制时,为什么这个视图上的按钮不可点击

4

6 回答 6

10

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

customView.userInteractionEnabled = NO;

然后添加到您的按钮。

[customButton addSubView:customView];

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

于 2016-03-04T23:16:31.887 回答
3

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

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

写上面如下解决了这个问题

    newViewBtn.frame =CGRectMake(5,5,60,20);
于 2013-11-06T06:03:01.533 回答
0

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

   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");
   }
于 2013-11-01T09:15:09.510 回答
0

你忘了这个:

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

请试试这个。

于 2013-11-01T09:15:45.733 回答
0

我相信您忘记添加目标(单击按钮时调用的函数):

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

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

- (void) yourfunction {
    NSLog(@"Click Detected");
}
于 2013-11-01T09:19:38.893 回答
0

试试下面的代码...

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");
}
于 2013-11-01T09:31:47.613 回答