2013-12-23 48 views
0

我的爲我的UITableViewCell供用戶選擇時間。我想按UITextField顯示UIDatePicker,但我的下面的代碼不起作用。點擊文本框仍然會顯示默認的鍵盤。iOS:UIDatePicker在點擊UITextView時不顯示

EditQuotaCell.h

@interface EditQuotaCell : UITableViewCell 

@property (nonatomic, weak) id delegate; 

@property (nonatomic, strong) IBOutlet UILabel *lblTimeRange; 

@property (nonatomic, strong) IBOutlet UITextField *txtTimeStart; 
@property (nonatomic, strong) IBOutlet UITextField *txtTimeEnd; 

@property (nonatomic, strong) UIDatePicker *timePicker; 

@end 

EditQuotaCell.m

@implementation EditQuotaCell 

@synthesize lblTimeRange; 
@synthesize txtTimeStart, txtTimeEnd; 

@synthesize timePicker; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 

    } 
    return self; 
} 

- (id) initWithCoder:(NSCoder *)aDecoder 
{ 
    if(self = [super initWithCoder:aDecoder]) 
    { 
     self.timePicker = [[UIDatePicker alloc] init]; 

     self.timePicker.datePickerMode = UIDatePickerModeDate; 
     [self.timePicker setDate:[NSDate date]]; 
     [self.timePicker setMinuteInterval:30]; 

     self.txtTimeStart.inputView = self.timePicker; 
    } 

    return self; 
} 

...... 

回答

0

解決它在awakeFromNib移動設置inputView

- (void) awakeFromNib 
{ 
    self.txtTimeStart.inputView = self.timePicker; 
} 
0

請設置您的選擇器視圖作爲輸入查看您的文本字段中的viewDidLoad

txtTimeStart.inputView = timePicker;

+0

確實。但是因爲這是一個'UITableViewCell',它沒有'viewDidLoad'。我通過將它放入'awakeFromNib'來解決它。如果您可以更改答案,我可以將其標記爲正確。 – chongzixin

+0

但是你可以在索引路徑的行中使用它。您應該在單元格中爲您的文本字段提供參考。 – jailani

+0

在我的情況下,'UIDatePicker'是在'UITableViewCell'中指定的,它與'UITableViewController'文件是分開的。 – chongzixin

0

可能是你應該viewDidLoad:方法嘗試

嘗試設置inputView爲您UITextFieldviewDidLoad:方法

- (void)viewDidLoad 
{ 
    self.timePicker = [[UIDatePicker alloc] init]; 

    self.timePicker.datePickerMode = UIDatePickerModeDate; 
    [self.timePicker setDate:[NSDate date]]; 
    [self.timePicker setMinuteInterval:30]; 

    self.txtTimeStart.inputView = self.timePicker; 
}