2009-11-10 69 views
0

嗨,我正在開發一個應用程序,用戶需要輸入開始日期和結束日期以查看指定日期範圍內的交易。 請建議我在iphone中顯示它的最佳方式。 我看到了日期選擇器,但它佔據了我的大部分。iphone中的顯示日期

回答

2

您可以爲開始日期和結束日期添加字段,當用戶點擊其中一個字段時,使用選取器顯示詳細信息視圖。瞭解如何在日曆應用程序中輸入日期和時間。

我最近也看到一些應用程序,其中日期選取器顯示爲模式視圖(從底部滾動,如鍵盤)。然後它只佔用屏幕,而用戶實際上選擇一個日期並在完成後消失。

更新: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; 
} 
+0

確切地說,我想以同樣的方式,你能幫我看看你的例子嗎? – shreedevi 2009-11-10 05:38:35

+0

我已經更新了我的答案。我仍然在工作(沒有訪問到Mac或Xcode中),並在記事本中輸入這件事,但有一次我回家,我會看一些類似的代碼我寫了我的項目之一,並更新答案,如果必要。不過,上述應該給你一個很好的起點。 – 2009-11-10 06:35:36

+0

當我點擊文本firld,我可以看到鍵盤輸入文字,但其背後的dateviewPicker來了.. 我不希望被看到,鍵盤當我點擊文本字段 – shreedevi 2009-11-10 09:23:15

1

訪問HIG,看看它有什麼話要說。 iPhone HIG DOC

+0

日期選擇器是好的,但它似乎佔據整個屏幕。請你可以幫我解答托馬斯穆勒給出的答案,這似乎很好。 – shreedevi 2009-11-10 05:37:31