使用自定义大小创建UIActionsheet

时间:2012-04-04 07:34:48

标签: iphone objective-c

我试图用UIDatepicker创建一个动作表,当按钮点击时,这个动作表是打开的,我想制作像popover一样的动作表,但是这不能正常工作

 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我需要做什么?

3 个答案:

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

答案 1 :(得分:0)

在我的案例中,我已尝试过您的代码和setBounds更改操作表的大小。

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

- (void)didPresentActionSheet:(UIActionSheet *)actionSheet

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

答案 2 :(得分: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;
这对我有用。恩乔伊