2013-07-13 72 views
0

我創建了一個彈出式按鈕時彈出的故事板中的datepicker。在選擇日期並按下完成按鈕後,日期必須發送至UIViewControlleruipopover與uidatepicker代理不起作用

我已經設置了協議和委託方法,如uidatepicker in uipopover delegate所解釋的,但我的viewcontroller中的委託方法從未調用。我錯過了什麼嗎?我需要爲此使用segues嗎?請幫忙!!

- (IBAction)doneButtonPress:(UIBarButtonItem *)sender { 
    NSLog(@"donebuttonpress"); 
    [_delegate receiveDate:pickedDate]; 
    NSLog(@"afterdelegation"); 
} 

兩者donebuttonpressafterdelegation進行打印,但是該方法從未被稱爲:(

//selectDatePopover.h

#import <UIKit/UIKit.h> 
@protocol selectDatePopoverDelegate <NSObject> 
    - (void)receiveDate:(NSString *)theDate; 
@end 

@interface selectDatePopover : UIViewController{ 
    id <selectDatePopoverDelegate> delegate; 
} 

@property (nonatomic, assign) id <selectDatePopoverDelegate> delegate; 
- (IBAction)doneButtonPress:(UIBarButtonItem *)sender; 
- (IBAction)mydatepicker:(UIDatePicker *)sender; 
@end 

//selectDatePopover.m

- (IBAction)doneButtonPress:(UIBarButtonItem *)sender { 
    NSLog(@"donebuttonpress"); 
    NSLog(_delegate); 
    [_delegate receiveDate:pickedDate]; 
    NSLog(@"afterdelegation"); 
} 

- (IBAction)mydatepicker:(UIDatePicker *)sender { 
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"yyyy-MM-dd"]; 
    pickedDate = [formatter stringFromDate:[sender date]]; 
    NSLog(pickedDate); 
} 

//CheckIn_ViewController.h

#import "selectDatePopover.h" 
@interface CheckIn_ViewController : UIViewController <selectDatePopoverDelegate> 
    @property UIPopoverController *popover; 
@end 

//CheckIn_ViewController.m

- (void)receiveDate:(NSString *)theDate { 
    NSLog(@"backhere"); 
    dateLabel.Text = theDate; 
} 

//在此按鈕的新聞,酥料餅有露面:

- (IBAction)calendarbutton:(UIButton *)sender { 
    UIView *anchor = sender; 
    selectDatePopover *selectDatePopoverInstance = 
    [self.storyboard instantiateViewControllerWithIdentifier:@"selectDatePopover"]; 
    _popover = [[UIPopoverController alloc] initWithContentViewController:selectDatePopoverInstance]; 
    [_popover presentPopoverFromRect:anchor.frame 
         inView:anchor.superview 
     permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 

    selectDatePopoverInstance.delegate = self; 
} 
+0

嘗試記錄_delegate並同時挑選日期以確保它們都不爲零。 – rdelmar

+0

pickedDate不是空的..我記錄它..但不知道如何登錄委託.. ?? – kate

+0

然後發佈您設置委託的代碼以及創建委託協議和委託屬性的.h文件。 – rdelmar

回答

1

這條線:self.popover.delegate =自;是不正確的。委託協議不在popover類中,它在其內容視圖控制器中,即selectDatePopover。因此,將該行更改爲:

selectDatePopover.delegate = self; 
+0

它返回一個錯誤:Property'委託'沒有找到' UIViewController *' – kate

+1

你需要在這一行改變UIViewController:「UIViewController * selectDatePopover = [self.storyboard ....」selectDatePopover(這是你的類名 - 它會更好,因爲它是大寫,但自你沒有,你可能想改變實例的名稱,使它們不一樣)。 – rdelmar

+0

警告消失..但我仍然無法讓代表工作:( – kate