2012-09-24 60 views
1

我想顯示一個日期選擇器,用戶只能輸入信用卡到期日的月份和年份。是否有任何選擇器只顯示月份和年份選項?iPhone:只有月份和年份字段的日期選擇器

+1

你可以試試下面的鏈接:http://stackoverflow.com/questions/3348247/uidatepicker-select-month-and-year –

回答

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 
+0

當人選擇一個月/年時,我們如何使用CDatePickerViewEx來更改文本字段的值? –

+0

嗨吉爾 - 鐵人,我怎樣才能使用這段代碼。你能給這個代碼的確切鏈接嗎? – Mani

相關問題