Saturday, 2 June 2012

how to make textfield on click show date picker and picker view with toolbar in iphone sdk

My Facebook

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    [textField resignFirstResponder];
   
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
   
    [textField resignFirstResponder];
   
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    //    [textField setInputAccessoryView:keyboardToolbar];
    if (textField==txtDate) {
        [txtDate resignFirstResponder];
        //        txtDate.inputView = datePickerl;
        [self showDate];
        }
    else if(textField==txtTime) {
        [txtTime resignFirstResponder];
        isPicker = true;
        [self showPicker];
        }
    else if (textField==txtCountry) {
        [txtCountry resignFirstResponder];
        isPicker=false;
        [self showPicker];
        }
    else if(textField==txtVanue) {
        [scrollView setContentOffset:CGPointMake(0, 100) animated:YES];
    }
    else if (textField==txtCountry) {
        [scrollView setContentOffset:CGPointMake(0, 120) animated:YES];
    }
}

- (void)LabelChange:(id)sender{
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateStyle = NSDateFormatterMediumStyle;
    txtDate.text = [NSString stringWithFormat:@"%@",
                       [df stringFromDate:datePicker.date]];
}

-(void)showDate{
    menu = [[UIActionSheet alloc] initWithTitle:@"Select Date"
               delegate:self
               cancelButtonTitle:nil
               destructiveButtonTitle:nil
               otherButtonTitles:nil];
   
    datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
    datePicker.datePickerMode = UIDatePickerModeDate;
    datePicker.minimumDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
    [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]autorelease]];
    [dateFormatter setDateFormat:@"dd MMM yyyy"];
    //[dateFormatter setDateFormat:@"MM/dd/YYYY"];
   
    //[theDatePicker release];
    [datePicker addTarget:self action:@selector(LabelChange:) forControlEvents:UIControlEventValueChanged];
   
   
    dateTool = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    dateTool.barStyle=UIBarStyleBlackOpaque;
    [dateTool sizeToFit];
    NSMutableArray *barItems = [[NSMutableArray alloc] init];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DatePickerDoneClick)];
    [barItems addObject:flexSpace];
   
    [dateTool setItems:barItems animated:YES];
    [menu addSubview:dateTool];
    [menu addSubview:datePicker];
    [menu showInView:self.view];
    [menu setBounds:CGRectMake(0,0,320, 464)];
   
   
    //    [self.view addSubview:datePicker];
   
    [datePicker release];
}
-(IBAction)DatePickerDoneClick{
    [menu dismissWithClickedButtonIndex:0 animated:YES];
}

-(void)showPicker{
    menu = [[UIActionSheet alloc] initWithTitle:@"Select Time"
               delegate:self
               cancelButtonTitle:nil
               destructiveButtonTitle:nil
               otherButtonTitles:nil];
   
    pickerView=[[UIPickerView alloc] init];
    pickerView.frame= CGRectMake(0.0, 44.0, 0.0, 0.0);
    pickerView.delegate=self;
    pickerView.dataSource=self;
    pickerView.showsSelectionIndicator = YES;
    txtTime.inputView=pickerView; 
   
    dateTool = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    dateTool.barStyle=UIBarStyleBlackOpaque;
    [dateTool sizeToFit]; 
    NSMutableArray *barItems = [[NSMutableArray alloc] init];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DatePickerDoneClick)];
    [barItems addObject:flexSpace];
   
    [dateTool setItems:barItems animated:YES];
    [menu addSubview:dateTool];
    [menu addSubview:pickerView];
    [menu showInView:self.view];
    [menu setBounds:CGRectMake(0,0,320, 464)];
    [pickerView release];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (isPicker==true) {
        return [arrTime count];
        }
    else {
        return [arrCountry count];
        }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (isPicker==true) {
        return [arrTime objectAtIndex:row];
        }
    else {
        return [arrCountry objectAtIndex:row];
        }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (isPicker==true) {
        txtTime.text = [arrTime objectAtIndex:row];
        }
    else {
        txtCountry.text = [arrCountry objectAtIndex:row];
        }
   
}

1 comment:

  1. -------------------
    Thanks for sharing this information i am bookmark this blog. need more post
    ------------------------------
    iPhone App Development And
    iOS App Development And

    ReplyDelete