1

I am coding a TableViewController with a Section called Information where the user enter a name , a date , a description. My problem is that after i've done all the settings for the NSDate picker on the date's textfield , when i click on the description's textfield instead of keyboard i see the date picker and it stores date in the date's textfield.

.h file

@interface ViewControllerInfo : UITableViewController <UITableViewDelegate, UITextFieldDelegate>{
NSDate *date;
UIActionSheet *dateSheet;
}

@property (weak, nonatomic) IBOutlet UITextField *scadenza;
@property (weak, nonatomic) IBOutlet UITextField *dateTextField;
@property (nonatomic, retain)NSDate *date;

-(void)setDatario;
-(void)dismissDateSet;
-(void)cancelDate;

.m file (pointing only the functions needed):

-(void)setDatario;{

dateSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil      destructiveButtonTitle:nil otherButtonTitles:nil];

[dateSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

CGRect pickerFrame = CGRectMake(0, 44, 0, 0);

UIDatePicker *datePicker = [[UIDatePicker alloc]initWithFrame:pickerFrame];
[datePicker setDatePickerMode:UIDatePickerModeDate];

[dateSheet addSubview:datePicker];

UIToolbar *controlToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, dateSheet.bounds.size.width, 44)];

[controlToolbar setBarStyle:UIBarStyleBlack];
[controlToolbar sizeToFit];

UIBarButtonItem *spacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

UIBarButtonItem *setButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissDateSet)];

UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelDate)];

[controlToolbar setItems:[NSArray arrayWithObjects:spacer, setButton, cancelBtn, nil] animated:NO];


[dateSheet addSubview:controlToolbar];

[dateSheet showInView:self.view];

[dateSheet setBounds:CGRectMake(0, 0, 320, 485)];

}

-(void)cancelDate{

[dateSheet dismissWithClickedButtonIndex:0 animated:YES];

}

-(void)dismissDateSet{

NSArray *listofViews = [dateSheet subviews];

for (UIView *subview in listofViews){
    if ([subview isKindOfClass:[UIDatePicker class]]){
        self.date = [(UIDatePicker*)subview date];

}

    [dateSheet dismissWithClickedButtonIndex:0 animated:YES];
}

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];

[dateTextField setText:[dateFormatter stringFromDate:self.date]];

}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
[self setDatario];
return NO;
}

I couldn't understand why is interesting also the description's texfield. Question is : why the description's textfield is matched with the datepicker ?

0

2 Answers 2

0

Replace your textFieldShouldBeginEditing method like this

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

    if (textField == scadenza) {

        return YES;
    }
    else if(textField == scadenza){

        [self setDatario];

        return NO;

    }
    return YES;
}
1
  • if (textfield == self.dateTextField) { [self setDatario]; return YES; } else if(textField == self.scadenza) { return NO; } that way is perfect! Thank u so much!
    – MrKinisia
    Mar 7, 2014 at 12:57
0

Even better, put a custom UIButton (invisible) of the same size as the textfield right over the textfield. Set the textfield enabled attribute to NO and then show the picker when the button is tapped. When the user selects a date from the picker, update the text in the textfield. This way you won't have to manage the textfield's delegate. I have used both methods and the button is much cleaner and smoother for the user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.