2013-11-21 144 views
0

我正在使用Date選取器並且做得很好,但datepicker工具欄中DONE按鈕的操作不起作用。 請幫忙解決這個問題。UIBarbutton操作不與UIDatePicker一起工作

~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~

- (IBAction)timepicker:(id)sender { 
    timepicker.hidden = NO; 

    NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
    df.timeStyle = NSDateFormatterShortStyle; 
    timebtn.titleLabel.text = [NSString stringWithFormat:@"%@",[df stringFromDate:timepicker.date]]; 
    UIToolbar *toolbar = [[UIToolbar alloc] init]; 
    toolbar.barStyle = UIBarStyleBlackOpaque; 
    toolbar.frame=CGRectMake(0,4,timepicker.frame.size.width,40); 

    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked:)]; 

    [toolbar setItems:[NSArray arrayWithObject:doneBtn] animated:NO]; 

    [timepicker addSubview:toolbar]; 

} 

- (IBAction)pickerDoneClicked:(id)sender 
{ 
    NSLog(@"DONE IS PRESSED"); 
    [timepicker setHidden:YES]; 
} 
+0

請問我比「不工作」更具體嗎? –

+0

代碼檢查正常。嘗試在方法和調試上都放置一個斷點。張貼更多的代碼片段。也許問題出在其他地方 – staticVoidMan

+0

@staticVoidMan我也這樣做bt無法找到錯誤.... –

回答

0

使用此方法,而不是 - (IBAction爲)pickerDoneClicked:(ID)發送

- (void)pickerDoneClicked:(id)sender 
{ 
    NSLog(@"DONE IS PRESSED"); 
    [timepicker setHidden:YES]; 
} 
+0

@sahil,請留下評論,如果我的答案有幫助u – Ramdhas

0

IBAction是Interface Builder Action的縮寫,它是在界面構建器(xibs和storyboards編輯器)中定義的控件事件上調用的。雖然IBAction解決爲無效,但如果您已在代碼中定義了目標和操作,則控制器將無法識別它。

因此,使用void返回類型,如Ramdy所說。

- (void)pickerDoneClicked:(id)sender 
{ 
    NSLog(@"DONE IS PRESSED"); 
    [timepicker setHidden:YES]; 
} 
+0

我做到了這一點,但仍然無法正常工作...... ..... :( –

+0

你沒有得到NSLog? –

0

嘗試將其添加到子視圖之前加入兩者UIToolbarUIDatePicker(具有合適的幀大小)到UIView

我遇到了同樣的問題,事實證明,選擇器視圖與工具欄重疊,導致UIDatePicker劫持連接到UIBarButtonItem的選擇器。

// picker with the appropriate x,y,w,h (216 is default height of UIDatePicker) 
timepicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, self.view.bounds.size.width, 216)]; 
timepicker.datePickerMode = UIDatePickerModeTime; 
[timepicker addTarget:self action:@selector(timePickerValueChanged:) forControlEvents:UIControlEventValueChanged]; 

UIBarButtonItem *timePickerDoneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(timePickerDoneTapped:)]; 
UIBarButtonItem *timePickerFlex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 
// toolbar with appropriate x,y,w,h 
UIToolbar *timePickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)]; 
[timePickerToolbar setBarStyle:UIBarStyleDefault]; 
[timePickerToolbar sizeToFit]; 
[timePickerToolbar setItems:@[timePickerFlex,timePickerDoneButton] animated:YES]; 

// view with appropriate x,y,w,h 
UIView *timePickerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 260)]; 
[timePickerView addSubview:timePickerToolbar]; 
[timePickerView addSubview:timepicker];