新来的交互,竟然让做这样一个东西~
所以我把它封装了起来,方便调用。
PickerAlertView 自然是集成 UIAlertView
继承之后就可以修改frame并且添加控件到这个view上了,下面是代码。
@interface PickerAlertView : UIAlertView{
UIDatePicker *datePickerView;
}
@property (nonatomic,retain) UIDatePicker *datePickerView;
- (void)createDatePicker;
@end
#import "PickerAlertView.h"
@implementation PickerAlertView
@synthesize datePickerView;
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self createDatePicker];
}
return self;
}
- (void)setFrame:(CGRect)rect {
[super setFrame:CGRectMake(0, 0, 320, 300)];//width 默认 284
self.center = CGPointMake(320/2, 280);
}
- (void)layoutSubviews {
[super layoutSubviews];
for (UIView *view in self.subviews) {
if (view.frame.size.height == 43) {
view.frame = CGRectMake(view.frame.origin.x, 232, 127, 43);
}
}
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
}
#pragma mark -
#pragma mark UIPickerView - Date/Time
- (void)createDatePicker
{
datePickerView = [[UIDatePicker alloc] initWithFrame:CGRectZero];
datePickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
datePickerView.frame = CGRectMake(10, 10, 300, 216);//216
datePickerView.datePickerMode = UIDatePickerModeDate;
[self addSubview:datePickerView];
}
@end
在需要的地方调用 可以定义个aciton
- (void)pickerAction {
PickerAlertView *pickerAlertView = [[PickerAlertView alloc] initWithTitle:@" " message:@" " delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[pickerAlertView show];
}
在delegete里面抓取picker的时间
#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
PickerAlertView *alert2 = alertView;
// [dateFormatter stringFromDate:alert2.datePickerView.date];
NSString *dateFromData = [NSString stringWithFormat:@"%@",alert2.datePickerView.date];
NSString *date = [dateFromData substringWithRange:NSMakeRange(0, 10)];
label.text = date;
NSLog(@"date %@ ... %@",date,alert2.datePickerView.date);
}