多个 uipickerview 和 uidatapicker

发布于 2024-08-24 15:20:18 字数 3232 浏览 7 评论 0原文

我有一个包含许多选择的视图,例如 checkInDate、checkOutDate (uidatepicker) 和年龄,房间(uipickerView)...

我想使用 showActionSheet 在单击不同的 tableViewCell 时显示不同的 pickerview

但我遇到两个问题:

  1. 如果我使用 checkOutDate.date = nowDate; 则无法第二次显示 checkInDate 和 theCheckOutDate;
  2. 我如何在此视图中使用多选择器视图?它不应该是多个组件。

这是主要代码:

- (void)viewDidLoad {
    age = [[UIPickerView alloc] init];
    age.showsSelectionIndicator = YES;
    age.delegate = self;
    NSArray *ageData = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", nil];
    self.pickerData = data;

    //room = [[UIPickerView alloc] init];
    //room.showsSelectionIndicator = YES;
    //room.delegate = self;
    //NSArray *roomData = [[NSArray alloc] initWithObjects:@"6", @"7", nil];
    //self.pickerData = roomDate;

//check data
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.day = 1;
    NSDate *now = [NSDate date];
    NSDate *nextDay = [gregorian dateByAddingComponents:components toDate:now options:0];
    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
    formatter.dateFormat = @"yyyy-MM-dd";
    checkInDateStr = [formatter stringFromDate:nextDay];
    NSDate *dayAfterTom = [gregorian dateByAddingComponents:components toDate:nextDay options:0];
    checkOutDateStr = [formatter stringFromDate:dayAfterTom];

    checkInDate = [[UIDatePicker alloc] init];
    checkInDate.tag = checkInDateTag;
    [checkInDate setMinimumDate:now];
    checkInDate.datePickerMode = UIDatePickerModeDate;
    //checkInDate.date = nextDay;
    checkOutDate = [[UIDatePicker alloc] init];
    checkOutDate.tag = checkOutDateTag;
    [checkOutDate setMinimumDate:nextDay];
    checkOutDate.datePickerMode = UIDatePickerModeDate;
    //checkOutDate.date = dayAfterTom;
}

- (void)showActionSheet:(NSIndexPath *)indexPath withDataPickerTag:(NSInteger *)tag{
    NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @"\n\n\n\n\n\n\n\n\n" : @"\n\n\n\n\n\n\n\n\n\n\n\n" ;

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:nil otherButtonTitles:@"done", nil];
    [actionSheet showInView:self.view];
    //UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease];
    //  datePicker.tag = 101;
    //  datePicker.datePickerMode = UIDatePickerModeDate;
    //  NSDate *now = [NSDate date];
    //  [datePicker setDate:now animated:YES];
    if (tag == 201){
        [actionSheet addSubview:checkInDate];
    }else if (tag == 202){
        [actionSheet addSubview:checkOutDate];
    }
}

#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [pickerData count];
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [pickerData objectAtIndex:row];
}

i have a view with many select like checkInDate, checkOutDate (uidatepicker)
and age, room(uipickerView)...

i want to use showActionSheet to display the different pickerview when click diffrent tableViewCell

but i got two problem:

  1. the checkInDate and theCheckOutDate can not be show the second time if i use checkOutDate.date = nowDate;
  2. how can i use multi pickerview in this view? it should not be multiple Components.

here is the main code:

- (void)viewDidLoad {
    age = [[UIPickerView alloc] init];
    age.showsSelectionIndicator = YES;
    age.delegate = self;
    NSArray *ageData = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", nil];
    self.pickerData = data;

    //room = [[UIPickerView alloc] init];
    //room.showsSelectionIndicator = YES;
    //room.delegate = self;
    //NSArray *roomData = [[NSArray alloc] initWithObjects:@"6", @"7", nil];
    //self.pickerData = roomDate;

//check data
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.day = 1;
    NSDate *now = [NSDate date];
    NSDate *nextDay = [gregorian dateByAddingComponents:components toDate:now options:0];
    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
    formatter.dateFormat = @"yyyy-MM-dd";
    checkInDateStr = [formatter stringFromDate:nextDay];
    NSDate *dayAfterTom = [gregorian dateByAddingComponents:components toDate:nextDay options:0];
    checkOutDateStr = [formatter stringFromDate:dayAfterTom];

    checkInDate = [[UIDatePicker alloc] init];
    checkInDate.tag = checkInDateTag;
    [checkInDate setMinimumDate:now];
    checkInDate.datePickerMode = UIDatePickerModeDate;
    //checkInDate.date = nextDay;
    checkOutDate = [[UIDatePicker alloc] init];
    checkOutDate.tag = checkOutDateTag;
    [checkOutDate setMinimumDate:nextDay];
    checkOutDate.datePickerMode = UIDatePickerModeDate;
    //checkOutDate.date = dayAfterTom;
}

- (void)showActionSheet:(NSIndexPath *)indexPath withDataPickerTag:(NSInteger *)tag{
    NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @"\n\n\n\n\n\n\n\n\n" : @"\n\n\n\n\n\n\n\n\n\n\n\n" ;

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:nil otherButtonTitles:@"done", nil];
    [actionSheet showInView:self.view];
    //UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease];
    //  datePicker.tag = 101;
    //  datePicker.datePickerMode = UIDatePickerModeDate;
    //  NSDate *now = [NSDate date];
    //  [datePicker setDate:now animated:YES];
    if (tag == 201){
        [actionSheet addSubview:checkInDate];
    }else if (tag == 202){
        [actionSheet addSubview:checkOutDate];
    }
}

#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [pickerData count];
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [pickerData objectAtIndex:row];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那支青花 2024-08-31 15:20:18

您可以设置选取器视图的框架。

而不是

age = [[UIPickerView alloc] init];

使用

UIPickerView *picker = [[UIPickerView alloc] initWithFrame:<Your frame>];

[picker setFrame:<your frame>];

您可以使用多个选择器视图,

使用它。为两者保留一个标识符,以便您可以处理委托。

You can set the frame of a picker view.

Instead of

age = [[UIPickerView alloc] init];

Use

UIPickerView *picker = [[UIPickerView alloc] initWithFrame:<Your frame>];

or

[picker setFrame:<your frame>];

Using this you can use multiple picker views.

Keep an identifier to both so that you can handle the delegates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文