2010-03-08 65 views
0

我有许多视图中选择喜欢checkInDate,checkOutDate(的UIDatePicker) 和年龄,室(uipickerView)...多个uipickerview和uidatapicker

我想用showActionSheet显示不同pickerview当点击不同势tableViewCell

,但我有两个问题:

  1. 的checkInDate和theCheckOutDate不能显示第二次,如果我使用checkOutDate.date = nowDate;
  2. 如何在这个视图中使用多个pickerview?它不应该是多个组件。

这里是主要代码:

- (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]; 
} 

回答

1

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

而不是

age = [[UIPickerView alloc] init]; 

使用

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

[picker setFrame:<your frame>]; 

使用此您可以使用多个选择器的看法。

保留两者的标识符,以便您可以处理代表。