0

请检查代码

  -(IBAction)showDateSheet{
dateSheet=[[UIActionSheet alloc]initWithTitle:@"Select Test Date" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil];
datePicker=[[UIDatePicker alloc]init];
[datePicker addTarget:self action:@selector(changeDateInLabel:) forControlEvents:UIControlEventValueChanged];
dateSheet.delegate=self;
//[dateSheet setFrame:CGRectMake(0, 117, 320, 383)];
datePicker.datePickerMode=UIDatePickerModeDate;
CGRect pickerRect=datePicker.bounds;
pickerRect.origin.y=-100;
datePicker.bounds=pickerRect;
[dateSheet addSubview:datePicker];
[dateSheet showInView:self.view];
[dateSheet setBounds:CGRectMake(0.0f, 0.0f, 320, 500)];

//[datePicker release];
//[dateSheet release];

   }

通过使用上面的代码,我无法单击取消按钮,当我点击取消按钮时没有任何反应

4

2 回答 2

1

您的错误是datePicker这样设置界限:

CGRect pickerRect=datePicker.bounds;
pickerRect.origin.y=-100;
datePicker.bounds=pickerRect;

本质上,您所做的是移动选择器的绘制位置,而不是选择器的框架所在的位置。要更清楚地看到这一点,请将选择器的clipsToBounds属性设置为YES并查看您得到的内容。即使您没有设置clipsToBounds,您也无法与下面的部分进行交互。

您真正想要做的是将框架原点向下设置,而不是边界原点向上设置一定数量的点。工作代码看起来像这样:

CGRect pickerRect=datePicker.frame;
pickerRect.origin.y+=110;
datePicker.frame=pickerRect;
于 2012-04-10T20:06:50.223 回答
0

您需要在操作表上添加按钮,如下所示...

UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
closeButton.momentary = YES; 
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[dateSheet addSubview:closeButton];
[closeButton release];

这是在打电话之前完成的[dateSheet showInView:self.view];

然后在选择器中只写这个..

[actionSheet dismissWithClickedButtonIndex:0 animated:YES]
于 2012-04-10T19:37:36.333 回答