iPhone:日期选择器,只有月份和年份字段

时间:2012-09-24 06:05:48

标签: iphone datepicker

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

1 个答案:

答案 0 :(得分:0)

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

或者这是一个获得相同效果的解决方案。要使用这段代码,您应该将UIPickerView替换为“Indentity inspector”的“Custom class”中的nib文件中的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