【问题标题】:iPhone:Date Picker with only month and year fieldsiPhone:只有月份和年份字段的日期选择器
【发布时间】:2012-09-24 06:05:47
【问题描述】:

我想显示一个日期选择器,用户将只输入信用卡到期日的月份和年份。是否有任何选择器只显示月份和年份选项?

【问题讨论】:

标签: iphone datepicker


【解决方案1】:

您可能需要制作自己的选择器。但是,您不必将其子类化或其他任何东西;只需使用通用 UIPickerView 并从您的 UIPickerViewDelegate/UIPickerViewDataSource 方法返回适当的值。

或者这是获得相同效果的解决方案。要使用这个 sn-p 代码,您应该在“Indentity 检查器”的“自定义类”的 nib 文件中将 UIPickerView 替换为 CDatePickerViewEx。

.h 文件

#import <UIKit/UIKit.h>

@interface CDatePickerViewEx : UIPickerView <UIPickerViewDelegate, UIPickerViewDataSource> 

@property (nonatomic, strong, readonly) NSDate *date;
-(void)selectToday;

@end

.m 文件

#import "CDatePickerViewEx.h"


// Identifiers of components

    #define MONTH ( 0 )
    #define YEAR ( 1 )

// Identifies for component views

    #define LABEL_TAG 43


    @interface CDatePickerViewEx()

    @property (nonatomic, strong) NSIndexPath *todayIndexPath;
    @property (nonatomic, strong) NSArray *months;
    @property (nonatomic, strong) NSArray *years;

    -(NSArray *)nameOfYears;
    -(NSArray *)nameOfMonths;
    -(CGFloat)componentWidth;

    -(UILabel *)labelForComponent:(NSInteger)component selected:(BOOL)selected;
    -(NSString *)titleForRow:(NSInteger)row forComponent:(NSInteger)component;
    -(NSIndexPath *)todayPath;
    -(NSInteger)bigRowMonthCount;
    -(NSInteger)bigRowYearCount;
    -(NSString *)currentMonthName;
    -(NSString *)currentYearName;

@end

【讨论】:

  • 当人选择月/年时,我们如何使用CDatePickerViewEx改变文本字段的值?
  • 您好 Gill-The IronMan,我如何使用此代码。你能给出这个代码的确切链接吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-07
  • 2020-09-06
  • 1970-01-01
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多