0

我在情节提要中创建了一个带有日期选择器的弹出框,当按下按钮时会弹出。选择日期并按下完成按钮后,必须将日期发送到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;
}
4

1 回答 1

1

这一行:self.popover.delegate = self; 是不正确的。委托协议不在 popover 类中,它在其内容视图控制器中,即 selectDatePopover。因此,将该行更改为:

selectDatePopover.delegate = self;
于 2013-07-13T20:57:14.597 回答