我想通過點擊文本框來實現日期選擇器。我正在使用this link中提到的代碼。datepicker通過點擊textfield
但我沒有得到輸出。如果有人知道如何通過點擊文本框來使日期選擇器。請爲我提供一些解決方案。
在此先感謝。
我想通過點擊文本框來實現日期選擇器。我正在使用this link中提到的代碼。datepicker通過點擊textfield
但我沒有得到輸出。如果有人知道如何通過點擊文本框來使日期選擇器。請爲我提供一些解決方案。
在此先感謝。
我看到你的鏈接,並得到了解決,從而嘗試下面的代碼。
#import "TextfieldwithDatepickerViewController.h"
UIActionSheet *pickerViewPopup;
@implementation TextfieldwithDatepickerViewController
- (void)textFieldDidBeginEditing:(UITextField *)aTextField{
[aTextField resignFirstResponder];
pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
pickerView.datePickerMode = UIDatePickerModeDate;
pickerView.hidden = NO;
pickerView.date = [NSDate date];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];
[barItems addObject:doneBtn];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];
[barItems addObject:cancelBtn];
[pickerToolbar setItems:barItems animated:YES];
[pickerViewPopup addSubview:pickerToolbar];
[pickerViewPopup addSubview:pickerView];
[pickerViewPopup showInView:self.view];
[pickerViewPopup setBounds:CGRectMake(0,0,320, 464)];
}
-(void)doneButtonPressed:(id)sender{
//Do something here here with the value selected using [pickerView date] to get that value
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
}
-(void)cancelButtonPressed:(id)sender{
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
}
此代碼是絕對有幫助你,因爲我運行的代碼,它的做工精細。
真的非常感謝您幫助我,親愛的。它工作正常。非常非常感謝你。 –
種類的工作正常,但當我點擊完成並取消它沒有響應。只有輪子旋轉。 –
你可以把你的日期選擇器放在界面生成器的另一個視圖中......並將視圖放在主視圖(x = 0; y = 460)下。
所以,當你的文本字段成爲第一響應者(有這樣的一些委託方法 - textFieldDidBeginEditing :) ...只需要使用datePicker在期望的位置動畫視圖。
動畫可以做到像...
[UIView beginAnimations:@"some_random_name" context:nil];
//give your date picker view a new frame
[UIView setAnimationDuration:0.3f]; //or any other value
[UIView commitAnimations];
使用下面的代碼:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet showInView:self.tabBarController.tabBar];
[actionSheet setFrame:CGRectMake(0, 220, 320,350)];
pickerToolbar.barStyle = UIBarStyleDefault;
[pickerToolbar sizeToFit];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:[BuddyCalcAppDelegate getLocalvalue:@"Cancel"] style:UIBarButtonSystemItemCancel target:self action:@selector(cancel_clicked:)];
[barItems addObject:cancelBtn];
[cancelBtn release];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexSpace];
[flexSpace release];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:[BuddyCalcAppDelegate getLocalvalue:@"Done"] style:UIBarButtonItemStyleDone target:self action:@selector(done_clicked:)];
[barItems addObject:doneBtn];
[doneBtn release];
[pickerToolbar setItems:barItems animated:YES];
[actionSheet addSubview:pickerToolbar];
[barItems release];
[pickerToolbar release];
UIDatePicker *datePicker=[[UIDatePicker alloc]init];
if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
{
datePicker.frame=CGRectMake(0,32,480, 216);
[scrollView setContentOffset:CGPointMake(0, 60) animated:YES];
} else {
datePicker.frame=CGRectMake(0,44,320, 216);
}
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
[datePicker setMinuteInterval:5];
[datePicker setTag:10];
[actionSheet addSubview:datePicker];
[datePicker addTarget:self action:@selector(Result) forControlEvents:UIControlEventValueChanged];
[datePicker release];
-(BOOL) textFieldShouldBeginEditing:(UITextField *)txtField
{
[self createDatePicker];
}
組代表TextFeild也
檢查您設置的委託文本字段。 如果不是,這樣設置:
textField.delegate = self;
還是在廈門國際銀行增加連接的委託。
我們可以用UITextField的inputView和inputAccessoryView。 在.h文件中聲明,在.m文件中聲明@synthesize。
@property (nonatomic, retain) IBOutlet UIToolbar *_accessoryView;
@property (nonatomic, retain) IBOutlet UIDatePicker *_datePickerView;
將它們連接到.xib文件中的文件所有者,確保它不在self.view中。
- (void)viewDidLoad
{
_dateOfBirthTextField.inputView = self._datePickerView;
_dateOfBirthTextField.inputAccessoryView = self._toolbar;
}
還寫函數從選擇器
#pragma mark- UITextField with UIDatePicker-
- (IBAction)dateChanged:(id)sender {
UIDatePicker *picker = (UIDatePicker *)sender;
_dateOfBirthTextField.text = [NSString stringWithFormat:@"%@", picker.date];
}
- (IBAction)doneEditing:(id)sender {
[_dateOfBirthTextField resignFirstResponder];
}
將日期添加dateChanged:IBAction爲上的valueChanged:_datePicker的作用。 和doneEditing:IBAction工具欄上的barbutton動作。
把透明按鈕放在你的文本字段上,並在點擊事件上顯示datepicker。或者你可以使用textfields委託方法。 –
「輸出」是什麼意思?如何將所選日期作爲字符串放入文本字段中? – Mundi