我在情节提要中创建了一个带有日期选择器的弹出框,当按下按钮时会弹出。选择日期并按下完成按钮后,必须将日期发送到UIViewController
.
我已经设置了协议和委托方法,如uipopover 委托中的 uidatepicker 所解释的,但我的视图控制器中的委托方法从未被调用。我错过了什么吗?我需要为此使用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;
}