如何在检查子视图是否已经添加之后,使用相同的按钮操作来解除子视图?

时间:2012-10-30 04:41:40

标签: iphone ios ipad

我使用以下代码在点击按钮时将日期选择器视图添加到我的应用主视图。

- (IBAction)CalenderButton_Click:(id)sender
{
// code to add date picker -mydatepicker

//animating


    [self.view addSubview:myPicker];


    CGRect onScreenFrame=myPicker.frame;
     CGRect offScreenFrame=onScreenFrame;
     offScreenFrame.origin.y=self.view.bounds.size.height;
     myPicker.frame=offScreenFrame;
     [UIView beginAnimations:nil context:nil];
     [UIView setAnimationDuration:0.5f];
     myPicker.frame=onScreenFrame;
     [UIView commitAnimations];

}

我需要实现的两件事是,

1.这段代码从底部动画了视图,我怎么能从顶部动画?

2.如果我再次单击按钮添加日期选择器,我应检查视图是否已添加,如果是,则删除子视图。

提前致谢

1 个答案:

答案 0 :(得分:2)

对于问题2:使用布尔值检查日期选择器是否已在视图中。 isSubView需要BOOL进行检查,您的myPicker位于子视图中。

<强>更新:

- (IBAction)CalenderButton_Click:(id)sender
{

//animating

   if(!isSubview){
      isSubview = YES;
      [self.view addSubview:myPicker];
 /*    
      CGRect onScreenFrame=myPicker.frame;
      CGRect offScreenFrame=onScreenFrame;
      offScreenFrame.origin.y=self.view.bounds.size.height;
      myPicker.frame=offScreenFrame;
      [UIView beginAnimations:nil context:nil];
      [UIView setAnimationDuration:0.5f];
      myPicker.frame=onScreenFrame;
      [UIView commitAnimations];
 */    

// to slide your view from the top
     [myPicker setFrame:CGRectMake(0, -200, 320, 200)];
     [myPicker setBounds:CGRectMake(0, 0, 320, 200)];

     [UIView beginAnimations:nil context:NULL];
     [UIView setAnimationDuration:0.5f];
     [myPicker setFrame:CGRectMake(0, 0, 320, 200)];
     [UIView commitAnimations];

    }else{
      isSubview = NO;
      [myPicker removeFromSuperView];
    }
}