gpt4 book ai didi

iphone - 如何在 Action Sheet 中添加日期选择器?

转载 作者:可可西里 更新时间:2023-11-01 03:13:49 25 4
gpt4 key购买 nike

- (IBAction) showCatPicker {

if (self.catList !=nil) {
self.catList=nil;
[catList release];
}
self.catList = [[NSMutableArray alloc] init];

self.actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[self.actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
if(self.catPicker == nil) {
self.catPicker = [[UIPickerView alloc] initWithFrame:pickerFrame];

self.catPicker.showsSelectionIndicator = YES;
self.catPicker.dataSource = self;
self.catPicker.opaque = YES;
self.catPicker.multipleTouchEnabled = YES;
self.catPicker.userInteractionEnabled = YES;
self.catPicker.delegate = self;
}

[self.actionSheet addSubview:self.catPicker];

UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Select"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor colorWithRed:19.0/255 green:122.0/255 blue:53.0/255 alpha:1.0];
[closeButton addTarget:self action:@selector(dismissGenderPicker:) forControlEvents:UIControlEventValueChanged];

UISegmentedControl *cancelButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Cancel"]];
cancelButton.momentary = YES;
cancelButton.frame = CGRectMake(10, 7.0f, 50.0f, 30.0f);
cancelButton.segmentedControlStyle = UISegmentedControlStyleBar;
cancelButton.tintColor = [UIColor colorWithRed:19.0/255 green:122.0/255 blue:53.0/255 alpha:1.0];
[cancelButton addTarget:self action:@selector(cancelActionSheet) forControlEvents:UIControlEventValueChanged];

[self.actionSheet addSubview:cancelButton];
[self.actionSheet addSubview:closeButton];

[cancelButton release];
[closeButton release];
[self.actionSheet showInView:self.view];
[self.actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
[catPicker reloadComponent:0];
}

最佳答案

您绝对不应该使用 UIActionSheet 来保存 UIDatePicker,因为此功能已被弃用!

来自Documentation :

Subclassing Notes

UIActionSheet is not designed to be subclassed, nor should you add views to its hierarchy. If you need to present a sheet with more customization than provided by the UIActionSheet API, you can create your own and present it modally with presentViewController:animated:completion:.

Important: UIActionSheet is deprecated in iOS 8. (Note that UIActionSheetDelegate is also deprecated.) To create and manage action sheets in iOS 8 and later, instead use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet.

您可以非常轻松地创建一个 UIView 来保存 UIDatePicker 并根据需要为 View 设置动画。如果需要,您甚至可以向其添加 UIToolbar。

这是一个例子:

创建两个属性:

@property (strong, nonatomic) UIDatePicker *theDatePicker;
@property (strong, nonatomic) UIView *pickerView;

创建您的选择器,将其嵌入到 UIView 中并显示:

    -(void) createDatePickerAndShow {

UIToolbar *controlToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, pickerView.bounds.size.width, 44)];

[controlToolbar sizeToFit];

UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

UIBarButtonItem *setButton = [[UIBarButtonItem alloc] initWithTitle:@"Set" style:UIBarButtonItemStyleDone target:self action:@selector(dismissDateSet)];

UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelDateSet)];

[controlToolbar setItems:[NSArray arrayWithObjects:spacer, cancelButton, setButton, nil] animated:NO];


[theDatePicker setFrame:CGRectMake(0, controlToolbar.frame.size.height - 15, theDatePicker.frame.size.width, theDatePicker.frame.size.height)];

if (!pickerView) {
pickerView = [[UIView alloc] initWithFrame:theDatePicker.frame];
} else {
[pickerView setHidden:NO];
}


CGFloat pickerViewYpositionHidden = self.view.frame.size.height + pickerView.frame.size.height;

CGFloat pickerViewYposition = self.view.frame.size.height - pickerView.frame.size.height;

[pickerView setFrame:CGRectMake(pickerView.frame.origin.x,
pickerViewYpositionHidden,
pickerView.frame.size.width,
pickerView.frame.size.height)];
[pickerView setBackgroundColor:[UIColor whiteColor]];
[pickerView addSubview:controlToolbar];
[pickerView addSubview:theDatePicker];
[theDatePicker setHidden:NO];


[self.view addSubview:pickerView];

[UIView animateWithDuration:0.5f
animations:^{
[pickerView setFrame:CGRectMake(pickerView.frame.origin.x,
pickerViewYposition,
pickerView.frame.size.width,
pickerView.frame.size.height)];
}
completion:nil];

}

And to dismiss the DatePicker:

-(void) cancelDateSet {
CGFloat pickerViewYpositionHidden = self.view.frame.size.height + pickerView.frame.size.height;

[UIView animateWithDuration:0.5f
animations:^{
[pickerView setFrame:CGRectMake(pickerView.frame.origin.x,
pickerViewYpositionHidden,
pickerView.frame.size.width,
pickerView.frame.size.height)];
}
completion:nil];
}

关于iphone - 如何在 Action Sheet 中添加日期选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8354919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com