iphone中的显示日期
嗨,我正在开发一个应用程序,用户需要输入开始日期和结束日期以查看指定日期范围内的交易。 请建议我在iphone中显示它的最佳方式。 我看到了日期选择器,但它占据了我的大部分。iphone中的显示日期
您可以为开始日期和结束日期添加字段,当用户点击其中一个字段时,使用选取器显示详细信息视图。了解如何在日历应用程序中输入日期和时间。
我最近也看到一些应用程序,其中日期选取器显示为模式视图(从底部滚动,如键盘)。然后它只占用屏幕,而用户实际上选择一个日期并在完成后消失。
更新:OK,我没有在时刻访问Mac和在记事本中输入这一切了,所以它可能不会运行未经修改:
首先,我想创建一个自定义的UIViewController:
@interface MyDatePickerViewController : UIViewController <UIPickerViewDelegate> {
UITextfield *textfieldBeingEdited;
UIDatePicker *datePicker;
}
@property (nonatomic, assign) UITextfield *textfieldBeingEdited; // not sure about 'assign'
@property (nonatomic, retain) IBOutlet UIDatePicker *datePicker;
- (IBAction)dismiss:(id)sender;
@end
@implementation MyDatePickerViewController
@synthesize textfieldBeingEdited;
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle {
if (self = [super initWithNibName:nibName bundle:nibBundle]) {
// do other initializations, if necessary
}
return self;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// TODO: get the date from textfieldBeingEdited and set it in the UIDatePicker
// You'd want the correct date preset when the view is animated in
self.datePicker.date = /* the date */;
}
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
NSDate *date = self.datePicker.date;
// TODO: format the date
NSString *formattedDate = ...;
// update the text field
textfieldBeingEdited.text = formattedDate;
}
- (IBAction)dismiss:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
@end
当用户点击您的日期字段中的一个,你会创建一个UIViewController并显示为模态的视图:
UIViewController *datePickerViewController = [[MyDatePickerViewController alloc]
initWithNibName:@"nib name goes here" bundle:nil];
datePickerViewController.textfieldBeingEdited = /*the field with the start date or end date*/;
[self presentModalViewController:datePickerViewController animated:YES];
[datePickerViewController release];
关于笔尖文件中的一些评论:
- 文件拥有者的类名会 是MyDatePickerViewController
- 添加的UIDatePicker,其连接到日期选择器插座,并设置它的代表等,以文件的所有者
- 添加一个按钮,以便用户可以关闭该视图和它挂到-dismiss:出口在MyDatePickerViewController
更新2:为了防止从显示终端键盘aying,我让我的视图控制器成为UITextField的代表。然后,我在-textFieldShouldBeginEditing中呈现模态视图控制器:并返回NO。返回NO停止键盘显示:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
MyDatePickerViewController *modal = [[MyDatePickerViewController alloc] initWithNibName:nil bundle:nil];
modal.textfieldBeingEdited = self.dateField;
[self presentModalViewController:modal animated:YES];
[modal release];
return NO;
}
访问HIG,看看它有什么话要说。 iPhone HIG DOC
日期选择器是好的,但它似乎占据整个屏幕。请你可以帮我解答托马斯穆勒给出的答案,这似乎很好。 – shreedevi 2009-11-10 05:37:31
确切地说,我想以同样的方式,你能帮我看看你的例子吗? – shreedevi 2009-11-10 05:38:35
我已经更新了我的答案。我仍然在工作(没有访问到Mac或Xcode中),并在记事本中输入这件事,但有一次我回家,我会看一些类似的代码我写了我的项目之一,并更新答案,如果必要。不过,上述应该给你一个很好的起点。 – 2009-11-10 06:35:36
当我点击文本firld,我可以看到键盘输入文字,但其背后的dateviewPicker来了.. 我不希望被看到,键盘当我点击文本字段 – shreedevi 2009-11-10 09:23:15