2

I have used UIDatePicker on the UITextField. I have press UITextField to select the date using datepicker 1st time(App working correct), 2nd Time when I have press again on UITextField to select the new date set on UITextField using UIDatePicker then application is crashes.

I am not using UITableView, I am using simple view that's viewController.

Please Help me...

Thanks

 - (void)viewDidLoad
    {
        [super viewDidLoad];

           form=[[NSDateFormatter alloc]init];
        [form setDateFormat:@"MM-dd-yyyy"];
        datePicker=[[UIDatePicker alloc]init];//Date picker
        datePicker.frame=CGRectMake(0,0,320, 216);
        datePicker.backgroundColor = [UIColor whiteColor];

    }


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
        BOOL a;
        if (textField.tag==10) {


        UIViewController* popoverContent = [[UIViewController alloc] init]; //ViewController

        UIView *popoverView = [[UIView alloc] init];   //view
        popoverView.backgroundColor = [UIColor blackColor];


        datePicker.datePickerMode = UIDatePickerModeDate;
        [datePicker setMinuteInterval:5];
        [datePicker setTag:10];



        [datePicker addTarget:self action:@selector(Result) forControlEvents:UIControlEventValueChanged];
        [popoverView addSubview:datePicker];

        popoverContent.view = popoverView;
            UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:popoverContent];
            navigationController.delegate=self;


            popoverController = [[UIPopoverController alloc]
                                      initWithContentViewController:navigationController];
        popoverController.delegate=self;


        [popoverController setPopoverContentSize:CGSizeMake(320, 250) animated:NO];


        [popoverController presentPopoverFromRect:textField.frame inView:self.addView permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
                        a=NO;

    }
        else
        {
            a=YES;
        }
        return a;
    }

 -(void)Result
    {
        NSDateFormatter *formDay = [[NSDateFormatter alloc] init];
        formDay.dateFormat=@"MM-dd-yyyy";
        NSString *day = [formDay stringFromDate:[datePicker date]];
        self.edit_dob.text = day;
      }

Error

*** Assertion failure in -[UIPickerTableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:], /SourceCache/UIKit/UIKit-3318.16.14/UITableView.m:9269
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource is not set'

I have share a screenshot. I have select date first time successfully and when I have press to reset new date on that textfield that time app crash.

enter image description here

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
Rahul Saini
  • 921
  • 2
  • 10
  • 23

2 Answers2

2
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker setMinuteInterval:5];
[datePicker setTag:10];

[datePicker addTarget:self action:@selector(Result) forControlEvents:UIControlEventValueChanged];
[popoverView addSubview:datePicker];

You are repeating these operations every time the textfield is focused. This practice is plain wrong, and is a potential cause of various unexpected behavior like the one you encountered. The fact that it works for the first time makes me think that one of these unnecessarily repeated actions is the culprit. (I'd bet on the addTarget:forControlEvents: method call. This should never be done in any case. I don't even know what happens when you try to add the same target again.)

You should structure your code better to make these method calls only once, instead of multiple times like in the code you've shown (e.g. in viewDidLoad instead of textFieldShouldBeginEditing:) and most probably, the error will go away.

Şafak Gezer
  • 3,928
  • 3
  • 47
  • 49
1

You need to retain the datepicker object i.e.:

@property(nonatomic,strong) UIDatePicker *datePicker;

in the .h file.

Here is updated code:

 - (void)viewDidLoad
 {
    [super viewDidLoad];

    form=[[NSDateFormatter alloc]init];
    [form setDateFormat:@"MM-dd-yyyy"];
    _datePicker=[[UIDatePicker alloc]init];//Date picker
    _datePicker.frame=CGRectMake(0,0,320, 216);
    _datePicker.backgroundColor = [UIColor whiteColor];
    _datePicker.datePickerMode = UIDatePickerModeDate;
    [_datePicker setMinuteInterval:5];
    [_datePicker setTag:10];
    [_datePicker addTarget:self action:@selector(Result)    forControlEvents:UIControlEventValueChanged];

 }


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


    BOOL a;
    if (textField.tag==10) {


        UIViewController* popoverContent = [[UIViewController alloc] init]; //ViewController
        UIView *popoverView = [[UIView alloc] init];   //view
        popoverView.backgroundColor = [UIColor blackColor];
        [popoverView addSubview:_datePicker];
        popoverContent.view = popoverView;
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:popoverContent];
        navigationController.delegate=self;

        UIPopoverController *popoverController = [[UIPopoverController alloc]
                                 initWithContentViewController:navigationController];
        popoverController.delegate=self;
        [popoverController setPopoverContentSize:CGSizeMake(320, 250) animated:NO];

        [popoverController presentPopoverFromRect:textField.frame inView:textField permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
        a=NO;

    }
    else
    {
        a=YES;
    }
    return a;
}

 -(void)Result
 {
    NSDateFormatter *formDay = [[NSDateFormatter alloc] init];
    formDay.dateFormat=@"MM-dd-yyyy";
    NSString *day = [formDay stringFromDate:[_datePicker date]];
    _edit_dob.text = day;
 }
Şafak Gezer
  • 3,928
  • 3
  • 47
  • 49
  • You are repeating these operations every time the textfield is focused. datePicker.datePickerMode = UIDatePickerModeDate; [datePicker setMinuteInterval:5]; [datePicker setTag:10]; [datePicker addTarget:self action:@selector(Result) forControlEvents:UIControlEventValueChanged]; – Girijesh Kumar Dec 26 '14 at 12:06