【问题标题】:error in using date picker使用日期选择器时出错
【发布时间】:2014-05-08 09:20:21
【问题描述】:

应用程序在调用此函数时崩溃了,但在 iPhone 中它工作正常,但在 iPad 中应用程序崩溃并说

注意:我正在使用 ActionSheetDatePicker 库

线程 1:EXC_BAD_ACCESS(cod = 1,地址 = 0x9e...)

当我调试时,我发现 selectedDate 在 iPad 中为 nil,但在 iPhone 中可以正常工作

#pragma mark - Implementation
- (void)dateWasSelected:(NSDate *)selectedDate element:(id)element {
//    self.selectedDate = selectedDate;

    //may have originated from textField or barButtonItem, use an IBOutlet instead of element

    //DATE TEXT FIELD HERE


    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    //Optionally for time zone converstions
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]];
    NSString *stringFromDate = [formatter stringFromDate:selectedDate];
    self.textFieldBirthDate.text = stringFromDate ;
    birthDate = stringFromDate ;

    //"YYYY-MM-DD"  the birthdate format

}

【问题讨论】:

  • 一般情况下,ActionSheet 默认使用 UIPopover,因此您会遇到困难,因此请您发布更多代码,以便调试。您也可以通过检查所选日期是否为 nil 来避免此崩溃,如果为 nil 则返回当前日期
  • 我正在使用这个库github.com/TimCinel/ActionSheetPicker 并且代码在他没有进入函数的函数行上崩溃了,它只是在函数头上得到这条绿线并停止,它的iPhone的相同代码,我只是将故事板从iPhone复制到iPad并调整大小,相同的代码我不知道会发生什么

标签: ios datepicker


【解决方案1】:

您提供的 git hub 代码对我来说运行良好,我确实调试了代码。

当用户点击完成按钮(选择日期后)时,将执行以下代码。

- (void)notifyTarget:(id)target didSucceedWithAction:(SEL)action origin:(id)origin {

    if ([target respondsToSelector:action])

        objc_msgSend(target, action, self.selectedDate, origin);

    else

        NSAssert(NO, @"Invalid target/action ( %s / %s ) combination used for ActionSheetPicker", object_getClassName(target), (char *)action);

}

现在确保您获得 self.selectedDate 的价值,如果您没有获得价值,请检查您是否在通过滚动选择日期时获得了以下代码的断点。

- (void)eventForDatePicker:(id)sender {

    if (!sender || ![sender isKindOfClass:[UIDatePicker class]])

        return;

    UIDatePicker *datePicker = (UIDatePicker *)sender;

    self.selectedDate = datePicker.date;

}

这些代码存在于文件中

ActionSheetDatePicker.m

如果您在此处获取断点,请确保您保存了日期。一旦您检查了这一点,问题将得到解决。

问候, 阿尼尔

【讨论】:

  • 一旦成功,请接受答案,否则请告诉我。
猜你喜欢
  • 2023-04-09
  • 2023-03-16
  • 2021-08-31
  • 2016-02-16
  • 2014-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多