Datepicker在textfield iOS 8上崩溃

时间:2014-12-26 10:59:25

标签: ios objective-c iphone ios8 uidatepicker

我在UIDatePicker上使用了UITextField。我已按UITextField使用datepicker第一次选择日期(App工作正确),第二次在UITextField再次按下时使用{{UITextField选择新日期1}}然后应用程序崩溃。

我没有使用UIDatePicker,我使用的是UITableView的简单视图。

请帮助我......

由于

viewController

错误

 - (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;
      }

我分享了截图。我已成功第一次选择日期,并且当我按下该文本字段时按此重置新日期时应用程序崩溃。

enter image description here

2 个答案:

答案 0 :(得分:2)

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

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

每次文本字段聚焦时,您都在重复这些操作。这种做法是完全错误的,并且是您遇到的各种意外行为的潜在原因。它第一次运作的事实使我认为这些不必要的重复行动中的一个是罪魁祸首。 (我打赌addTarget:forControlEvents:方法调用。在任何情况下都不应该这样做。我甚至不知道当你再次尝试添加相同目标时会发生什么。)

您应该更好地构建代码,使这些方法调用只进行一次,而不是像您已经显示的代码那样多次(例如在viewDidLoad中而不是textFieldShouldBeginEditing:)并且很可能,错误将消失。

答案 1 :(得分:1)

您需要保留datepicker对象,即:。

@property(nonatomic,strong) UIDatePicker *datePicker;
<。>文件中的

这是更新的代码:

 - (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;
 }