添加按钮
问题描述:
我创建包含在父视图的底部3个按钮隐藏的表单,通过按下另一个按钮,视图变得可见,但是那些3个按钮没有按下。我不太知道什么问题是。添加按钮
我通过代码添加一个按钮。这是按下按钮时调用的方法。
- (IBAction) addData:(id)sender
{
if(attachInfo==nil){
float screenHeight = self.view.frame.size.height;
attachInfo=[[UIView alloc] initWithFrame:CGRectMake(0, screenHeight, 320, 216)];
UIButton *attachImage = [UIButton buttonWithType:UIButtonTypeCustom];
//attachImage.enabled = YES;
[attachImage addTarget:self action:@selector(addImage:) forControlEvents:UIControlEventTouchUpInside];
[attachImage setFrame:CGRectMake(20, 20, 0, 0)];
[attachImage setImage:[UIImage imageNamed:@"add_photos"] forState:UIControlStateNormal];
[attachImage sizeToFit];
[attachInfo addSubview:attachImage];
UIButton *attachDoc = [UIButton buttonWithType:UIButtonTypeCustom];
//[pushButton addTarget:self action:@selector(pushViewController) forControlEvents:UIControlEventTouchUpInside];
[attachDoc setFrame:CGRectMake(120, 20, 80, 80)];
[attachDoc setImage:[UIImage imageNamed:@"add_docs.png"] forState:UIControlStateNormal];
[attachDoc sizeToFit];
[attachInfo addSubview:attachDoc];
UIButton *attachPlace = [UIButton buttonWithType:UIButtonTypeCustom];
//[pushButton addTarget:self action:@selector(pushViewController) forControlEvents:UIControlEventTouchUpInside];
[attachPlace setFrame:CGRectMake(220, 20, 80, 80)];
[attachPlace setImage:[UIImage imageNamed:@"add_place.png"] forState:UIControlStateNormal];
[attachPlace sizeToFit];
[attachInfo addSubview:attachPlace];
//[self.view addSubview:attachInfo];
[self.view addSubview:attachInfo];
[UIView animateWithDuration:0.3f animations:^{
CGRect frame = attachInfo.superview.frame;
frame.origin.y -= 216;
attachInfo.superview.frame=frame;
}];
}
else
{
[UIView animateWithDuration:0.3f animations:^{
CGRect frame = attachInfo.superview.frame;
frame.origin.y += 216;
attachInfo.superview.frame=frame;
}];
attachInfo=nil;
}
}
答
你需要添加按钮的作用。
- (IBAction)first:(id)sender;
{
firstViewController * fvc=[FirstViewController alloc]init];
[FirstButton setHidden:False];
[self.navigationController pushViewController:fvc animated:YES];
}
- (IBAction)Second:(id)sender;
{
SecondViewController * svc=[SecondViewController alloc]init];
[SecondButton setHidden:False];
[self.navigationController pushViewController:svc animated:YES];
}
- (IBAction)Third:(id)sender;
{
ThirdViewController * tvc=[ThirdViewController alloc]init];
[ThirdButton setHidden:False];
[self.navigationController pushViewController:fvc animated:YES];
}
答
在第一个按钮(attachImage)中,高度和宽度为零。所以它不应该是可见的。对于其他两个按钮,您尚未设置目标。所以那些是不可点击的。
为第一个按钮设置
[attachImage setFrame:CGRectMake(20, 20, 80, 80)];
和其他两个按钮添加
//for the second button
[attachDoc addTarget:self action:@selector(addImage:) forControlEvents:UIControlEventTouchUpInside];
//for the third button
[attachPlace addTarget:self action:@selector(addImage:) forControlEvents:UIControlEventTouchUpInside];
答
-(void)createHiddenForm
{
hiddenView = [[UIView alloc] initWithFrame:CGRectMake(0,100,320,300)];
[hidden setHidden:TRUE];
[self.view addSubview:hiddenView];
float posY = 50.0;
for (int i=0 ; i<3 ; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(50,posY, 100, 50)];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[hiddenView addSubview:button];
posY=posY+60;
}
}
-(void)showHiddenView
{
hidden.hidden = FALSE;
[self.view bringSubviewToFront:hiddenView];
}
-(void)buttonClicked:(id)sender
{
UIButton *senderBtn = (UIButton *)sender;
NSLog(@"Sender Button's tag = %@",senderBtn.tag);
}
希望它会帮助你。
答
float screenHeight = self.view.frame.size.height;
attachInfo=[[UIView alloc] initWithFrame:CGRectMake(0, screenHeight, 320, 216)];
从以上所述,您可以在视图边界之外添加attachInfo Y位置。 在视图外添加按钮onclick将不起作用。
使其工作取代下面的线。
[UIView animateWithDuration:0.3f animations:^{
CGRect frame = attachInfo.superview.frame;
frame.origin.y -= 216;
attachInfo.superview.frame=frame;
而不是移动超视图帧移动attachInfo框架就像这样。
[UIView animateWithDuration:0.3f animations:^{
CGRect frame = attachInfo.frame;
frame.origin.y -= 216;
attachInfo.frame=frame;
因此,attachInfo视图将被添加到超级视图边界与动画。
+0
如何使这项工作?我创建的屏幕外的视图,添加按钮和scrollView,并将其与动画块,超级视图一起移动,但没有任何元素不起作用(( – Max 2013-05-10 23:21:23
向我们展示一些代码上下工夫;) – Ushan87 2013-05-06 11:05:46
请发表您的用于显示view.Whether您添加编码或XIB这些按钮的代码?清楚解释。 – NSUserDefault 2013-05-06 11:05:49
邮政编码,你试过吗? – Rushabh 2013-05-06 11:05:49