1

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

4

1 回答 1

0

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

或者这是获得相同效果的解决方案。要使用此代码片段,您应该在“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
于 2012-09-24T06:09:04.413 回答