我創建了一個彈出式按鈕時彈出的故事板中的datepicker。在選擇日期並按下完成按鈕後,日期必須發送至UIViewController
。uipopover與uidatepicker代理不起作用
我已經設置了協議和委託方法,如uidatepicker in uipopover delegate所解釋的,但我的viewcontroller中的委託方法從未調用。我錯過了什麼嗎?我需要爲此使用segues嗎?請幫忙!!
- (IBAction)doneButtonPress:(UIBarButtonItem *)sender {
NSLog(@"donebuttonpress");
[_delegate receiveDate:pickedDate];
NSLog(@"afterdelegation");
}
兩者donebuttonpress
和afterdelegation
進行打印,但是該方法從未被稱爲:(
//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;
}
嘗試記錄_delegate並同時挑選日期以確保它們都不爲零。 – rdelmar
pickedDate不是空的..我記錄它..但不知道如何登錄委託.. ?? – kate
然後發佈您設置委託的代碼以及創建委託協議和委託屬性的.h文件。 – rdelmar