2011-12-13 87 views
1

所以我明白如何從日期選取器中取出日期就好了。但我似乎無法將它從UIActionSheet上的日期選擇器中拉出來。我們目前的設置是一種使用少數選取器輸入數據的表單,它們都使用數據源的didSelectRow方法,所有這些方法都使用相同的視圖來表示數據源。UIDatePicker,UIActionSheet和數據處理

現在這似乎不適用於UIDatePicker。這是我迄今爲止的日期選擇器,它與我的其他選取器類似,包括處理值更改的方法。

-(IBAction)ageBox{ 
    pickerAction=[[UIActionSheet alloc]initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; 
    [pickerAction setActionSheetStyle:UIActionSheetStyleBlackTranslucent]; 
    CGRect pickerFrame = CGRectMake(0, 40, 0, 0); 

    UIDatePicker *datePicker =[[UIDatePicker alloc]initWithFrame:pickerFrame]; 

    [pickerAction addSubview:datePicker]; 

    UISegmentedControl *closeButton=[[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObject:@"OK"] ]; 
    closeButton.momentary = YES; 
    closeButton.frame=CGRectMake(260, 7.0f, 50.0f, 30.0f); 
    closeButton.segmentedControlStyle =UISegmentedControlStyleBar; 
    closeButton.tintColor = [UIColor blackColor]; 
    date = datePicker.date; 
    [closeButton addTarget:self action:@selector(closePicker) forControlEvents:UIControlEventValueChanged]; 
    [pickerAction addSubview:closeButton]; 
    whatToChange = 5; 
    [pickerAction showInView:self.view]; 
    [pickerAction setBounds:CGRectMake(0, 0, 320, 464)]; 
} 

任何從日期選取器拉動的日期,而它的動作片,或當我它摧毀之前單擊確定,從它拉它的幫助嗎?

回答

1

我假設pickerActionselfivar,基於該方法中的第一行代碼。我可以想到幾種方法,我相信還有更多。

1)製作datePickerivar以及。這樣你就可以爲你的'ok'方法提供參考。

2)從pickerAction本身獲取參考。既然你已經添加了datePicker作爲subview,你可以像這樣找到它:

// Somewhere in your "ok" method 
    for (UIView *view in pickerAction.subviews) { 
     if ([view isKindOfClass:[UIDatePicker class]]){ 
      UIDatePicker *picker = (UIDatePicker *)view; 
      NSDate *pickedDate = picker.date; 
     } 
    } 
+0

第二工作就像一個魅力。我添加了NSDateFormatter * df = [[NSDateFormatter alloc] init]; df.dateStyle = NSDateFormatterMediumStyle; date = [NSString stringWithFormat:@「%@」, [df stringFromDate:pickedDate]];爲了使它成爲我可以在其他地方設置的字符串。謝謝! –