UIViewControllerHierarchyInconsistency crashes app only in iOS 8 and Xcode 6

社会主义新天地 提交于 2019-12-04 06:47:14

Either you create date picker programatically or if you are using it as a dragged in component do the below given steps

1.Create Programatically

 datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
//NSString *maxDateString = @"01-Jan-2012";
NSString *theMinimumDate = @"01-Jan-1976";
// the date formatter used to convert string to date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// the specific format to use
dateFormatter.dateFormat = @"dd-MMM-yyyy";
// converting string to date
NSDate *theMaximumDate = [NSDate date];//[dateFormatter dateFromString: maxDateString];
NSDate *theminimumDate = [dateFormatter dateFromString: theMinimumDate];

// repeat the same logic for theMinimumDate if needed
// here you can assign the max and min dates to your datePicker
[datePicker setMaximumDate:theMaximumDate];//the max age restriction (if needed, or else dont use this line)
[datePicker setMinimumDate:theminimumDate]; //the min age restriction

// set the mode
[datePicker setDatePickerMode:UIDatePickerModeDate];
// Do any additional setup after loading the view from its nib.
[datePicker addTarget:self action:@selector(updateTextField:) forControlEvents:UIControlEventValueChanged];

// and finally set the datePicker as the input mode of your textfield
[Textfield setInputView:datePicker];

2.From Xib

[self.DatePickerView removeFromSuperview];
[Textfield setInputView:self.DatePickerView];
[Textfield setInputView:self.DatePickerView];
umairhhhs

date picker should not be child of any super view

Problem:

You have to ensure that the view you will assign to inputView or inputAccessoryView don't belong to any parent view. Maybe when you create these views from xib inside a ViewController, by default they are subviews of a superview.

Solution Tips:

Using method removeFromSuperview for the view you will assign to inputView or inputAccessoryView

see detail in this link

Error when adding input view to textfield iOS 8

mattlm

The base problem was likely caused by adding the date picker as a child view of the view controllers view.

Removing the following line would have prevented this:

[self.view addSubview:self.myDatePicker];

see https://stackoverflow.com/a/25831317/1871400

angraonhunt

The problem is you have to ensure all the inputAccessoryView do not belong to any parent view. Refer: Error when adding input view to textfield iOS 8.

user2511630
-(void)viewDidLoad
                {
    //init and allocate(my problem was addind nil object into view.         
    self.myDatePicker = [[UIDatePicker alloc]init];

    [self.myDatePicker setDatePickerMode:UIDatePickerModeCountDownTimer];

    //to get text off picker
    [self.myDatePicker addTarget:self action:@selector(textFieldDidEndEditing:) forControlEvents:UIControlEventValueChanged];

    //to hide picker
    _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
}


-(BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"HH:mm"];
    NSDate *date = self.myDatePicker.date;
    //or select pickerViewMode

    switch (textField.tag) {
        case 1: //pulls selected text into textField
            self.textField1.text = [dateFormat stringFromDate:date];
        break;
     }
}

-(void)hideKeyboard {
    //NSLog(@"6  hideKeyboard");
    [self.textField1 resignFirstResponder];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!