【问题标题】:Showing a UIDatePicker with a null Year value显示具有空年份值的 UIDatePicker
【发布时间】:2014-01-11 14:50:55
【问题描述】:

我正在使用 UIDatePickerModeDate 在我的应用程序中使用 UIDatePicker。我想使用与 iOS 联系人通讯录中相同样式的 DatePicker,您可以在其中设置不带年份的日期:

September | 11 | 2014
October | 12 | ----
November | 13 |

在 textField 中获取字符串:October 12

有什么想法吗?

【问题讨论】:

    标签: ios uidatepicker


    【解决方案1】:

    只需添加一个 UIPickerView(不是 UIDatePickerView)并将数据源(和委托)设置为您的类。然后在你的类中添加如下内容:

    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    {
        return 3;
    }
    
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    {
          switch (component) {
        case 0: // how many days? (you might find out how many days the month really had, instead of just returning 31
        {
    #if TARGET_IPHONE_SIMULATOR
    
            return 50000;
            // I don't now, what the exact Value for NSIntegerMax is, but 50000 should be enough for testing.
    #else
    
            return NSIntegerMax;
    #endif
    
            break;
        }
        case 1: // how many months to display?
        {
    #if TARGET_IPHONE_SIMULATOR
    
            return 50000;
    #else
    
            return NSIntegerMax;
    #endif
    
            break;
        }
        case 2:
        {
            return 100;  // how many years?
            break;
        }
    }
    return 0;
    }
    
    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
    if (component == 0) // days
        return [NSString stringWithFormat:@"%i",(int)row % 31 + 1];
    else if (component == 1) // month
    {
        NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"MM"];
        NSDate* myDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%i",(int)row % 12 + 1]];
    
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"MMMM"];
        return [formatter stringFromDate:myDate];
    }
    
    
            else if (component == 2 && row == ([pickerView numberOfRowsInComponent:2] - 1))
        return @"-----";
           else
           {
                // let's use NSDateFormatter to get the current year:
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"YYYY"];
    
            return [NSString stringWithFormat:@"%i",[[dateFormatter stringFromDate:[NSDate date]] integerValue] - ([pickerView numberOfRowsInComponent:2] - 1) + (row + 1)];
            }
    }
    

    结果如下所示:

    你可以实现

    - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component 
    

    为了改变每个组件的宽度。

    【讨论】:

    • 这对我有用。你能帮我把第一个和第二个组件做成无穷无尽的吗?我的意思是在结束后重复值。
    • 我在上面更改了答案。请注意,您不能让选择器“真正”无休止。它会在用户滚动 NSIntegerMax 后停止(这是一个超高的值,而且没有人会这样做)。我希望我能帮上忙。如果您能选择我的答案作为正确答案,那就太好了;)
    【解决方案2】:

    UIDatepickerModeDate 不会让您获得所需的日期格式。因此,要么使用 UIPickerView 并自定义它的日期,要么按照上述答案中的技巧进行操作。

    你也可以有兴趣阅读这篇文章。

    link 1

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 2014-09-18
      • 1970-01-01
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多