HI,我正在开发一个应用程序,用户需要输入开始日期和结束日期才能查看指定日期范围内的交易。 请建议我在iPhone上显示它的最佳方式。 我看到了日期选择器,但它占据了我的大部分scren ..
答案 0 :(得分:2)
您可以拥有开始日期和结束日期的字段,当用户点击其中一个字段时,会显示带有选择器的详细信息视图。查看您在日历应用程序中输入日期和时间的方式。
我最近也看到了一些应用程序,其中日期选择器显示为模态视图(从底部滚动,如键盘)。然后它只在用户实际选择日期时占据屏幕,并在完成后消失。
更新:好的,我目前无法访问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];
关于nib文件的一些评论:
更新2 :为防止键盘显示,我将视图控制器作为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;
}
答案 1 :(得分:1)
访问HIG,看看它有什么用。 iPhone HIG DOC