2

我试图用 UIDatepicker 制作一个动作表,当按钮单击时,这个动作表是打开的,我想制作像弹出框这样的动作表,但这不起作用

 UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"Pick Value"
                                                      delegate:self
                                             cancelButtonTitle:@"Done"
                                        destructiveButtonTitle:nil
                                             otherButtonTitles:nil];

    UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,180,0,0)];

    [menu addSubview:pickerView];
    [menu showInView:self.view.superview];

    //Change the height value in your CGRect to change the size of the actinsheet
    [menu setBounds:CGRectMake(0,0,400,400)];

    [pickerView release]; 
    [menu release]; 

问题是这里只有一行操作表显示,没有绑定 400*400 我需要做什么?

4

3 回答 3

0

我已经尝试了您的代码,并且 setBounds 在我的情况下更改了操作表的大小。

无论如何,您也可以尝试在委托的方法中更改工作表的框架

- (void)didPresentActionSheet:(UIActionSheet *)actionSheet

它将确保在您尝试更新框架时已经创建了操作表。

于 2012-04-04T07:54:12.137 回答
0

在 .m 文件上执行此操作

UIViewController *popoverContent = [[UIViewController alloc]init];
UIView *popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];

    datepiker= [[UIDatePicker alloc]init];
    [datepiker setFrame:CGRectMake(0, 0, 320, 216)];
    datepiker.datePickerMode=UIDatePickerModeDateAndTime;
    datepiker.hidden=NO;
    datepiker.minimumDate=[NSDate date];

    [self.view addSubview:datepiker];

    [datepiker release];
   // [datepiker addTarget:self action:@selector(changedDate:) forControlEvents:UIControlEventValueChanged];

    btn_add=[[UIButton alloc]initWithFrame:CGRectMake(115, 250, 100, 30)];
    [btn_add setTitle:@"Add" forState:UIControlStateNormal];
    [btn_add setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20]];
    [btn_add setBackgroundColor:[UIColor redColor]];
    [btn_add addTarget:self action:@selector(AddDate:) forControlEvents:UIControlEventTouchUpInside];
    [popoverView addSubview:btn_add];

    btn_cancel=[[UIButton alloc]initWithFrame:CGRectMake(115, 300, 100, 30)];
    [btn_cancel setTitle:@"Cancel" forState:UIControlStateNormal];
    [btn_cancel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20]];
    [btn_cancel setBackgroundColor:[UIColor redColor]];
    [btn_cancel addTarget:self action:@selector(CancelDate:) forControlEvents:UIControlEventTouchUpInside];
    [popoverView addSubview:btn_cancel];

    [popoverView addSubview:datepiker];
    [popoverView addSubview:btn_add];
    popoverContent.view = popoverView;
    popoverContent.contentSizeForViewInPopover = CGSizeMake(320,350);

    self.popoverController = [[UIPopoverController alloc]
                                  initWithContentViewController:popoverContent];
    [self.popoverController  presentPopoverFromRect:CGRectMake(400,-150, 320,220) 
                                                 inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
        }

    [popoverView release];
    [popoverContent release];

}

-(void)AddDate:(id)sender
{
}
-(void)CancelDate:(id)sender
{
[popoverController dismissPopoverAnimated:YES];

}

在 .h 文件上执行此操作并添加 UIPickerViewDelegate,UIPopoverControllerDelegate

{
UIPopoverController *popoverController;
UIDatePicker *datepiker;
UIPickerView *picker;
UIButton *btn_add;
UIButton *btn_cancel;
}
@property(nonatomic,retain)IBOutlet UIDatePicker *datepiker;
@property(nonatomic,retain)IBOutlet UIPickerView *picker;
@property(nonatomic,retain)UIPopoverController *popoverController;
@property(nonatomic,retain)UIButton *btn_add;
@property(nonatomic,retain)UIButton *btn_cancel;
-(void)AddDate:(id)sender;
-(void)CancelDate:(id)sender;

这对我有用。恩乔伊

于 2012-04-04T07:59:44.777 回答
0
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"Pick Value"
                                                  delegate:self
                                         cancelButtonTitle:@"Done"
                                    destructiveButtonTitle:nil
                                         otherButtonTitles:nil];

UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,180,0,0)];

[menu addSubview:pickerView];
[menu showInView:self.view.superview];

[menu setFrame:CGRectMake(0,0,320,400)]; //used setFrame instread of setBounds

[pickerView release]; 
[menu release]; 
于 2012-04-04T07:39:50.173 回答