在我的视图上添加按钮

时间:2013-05-06 11:03:49

标签: iphone ios objective-c uibutton

我在父视图底部创建一个包含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 个答案:

答案 0 :(得分: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];

    }

答案 1 :(得分: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];

答案 2 :(得分: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);
 }

希望它会对你有所帮助。

答案 3 :(得分: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视图添加到带有动画的超级视图边界。