开发者

multiple uipickerview and uidatapicker

开发者 https://www.devze.com 2022-12-22 01:09 出处:网络
i have a view with many select like checkInDate, checkOutDate (uidatepicker) and age, room(uipickerView)...

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 = [gre开发者_JAVA百科gorian 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];
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消