tags:

views:

787

answers:

2

HI, i am developing an application, where the user needs to enter the start date and end date to view the transactions within that specified date range. please suggest me the best way to display it in iphone. i saw the date picker , but it occupies most of my scren..

+2  A: 

Visit the HIG and see what it has to say. iPhone HIG DOC

StarShip3000
date picker is good, but it seems to occupy the entire screen. Please can you help me out with the answer given by thomas muller, it seems to be good.
shreedevi
+2  A: 

You could have fields for the start and end date and when the user taps on one of the fields, show a detail view with the picker. Checkout how you enter a date and time in the Calendar application.

I also recently saw some application where the date picker was shown as a modal view (scrolled in from the bottom, like a keyboard). Then it only occupies the screen while the user is actually choosing a date and disappears once she is done.

Update: OK, I don't have access to a Mac at the moment and typed this all up in notepad, so it probably won't work unmodified:

First, I'd create a custom 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

When the user taps on one of your date fields, you'd create a UIViewController and show it as a modal view:

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];

Some comments about the nib file:

  • the class name of File's Owner would be MyDatePickerViewController
  • add a UIDatePicker, connect it to the datePicker outlet, and set it's delegate and so on to File's Owner
  • add a button so the user can dismiss the view and hook it up to the -dismiss: outlet in MyDatePickerViewController

Update 2: To prevent the keyboard from displaying, I made my view controller the delegate for the UITextField. Then, I present the modal view controller in -textFieldShouldBeginEditing: and return NO. Returning NO stops the keyboard from showing:

- (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;
}
Thomas Müller
exactly, i want it the same way, can you just help me with the example you saw.
shreedevi
I've updated my answer. I'm still at work (with no access to a Mac or xcode) and typed this up in Notepad, but once I get home I'll have a look at some similar code I wrote for one of my projects and update the answer if necessary. The above should give you a good starting point, though.
Thomas Müller
when i click the text firld , i can see the keypad to enter text, but behind it the dateviewPicker is coming..i dont want that keypad to be seen when i click the text field
shreedevi
hi Thomas please help
shreedevi
I've updated the answer with an explanation of how to prevent the keyboard from showing.
Thomas Müller
thanks a lot.... it worked
shreedevi
Do you mind accepting the answer then? Thanks.
Thomas Müller