0

我在父视图的底部创建了一个包含 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;
    }



}
4

4 回答 4

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

而不是像这样移动 superview 框架移动 attachInfo 框架。

    [UIView animateWithDuration:0.3f animations:^{
        CGRect frame = attachInfo.frame;
        frame.origin.y -= 216;
        attachInfo.frame=frame;

这样 attachInfo 视图将被添加到带有动画的超级视图边界。

于 2013-05-06T13:40:09.970 回答
0

您需要添加按钮的操作。

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

    }
于 2013-05-06T11:11:51.283 回答
0
 -(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);
 }

希望它会帮助你。

于 2013-05-06T11:24:32.573 回答
0

在第一个按钮 (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];
于 2013-05-06T11:23:02.550 回答