【发布时间】: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