uipopover与uidatepicker委托不工作

时间:2013-07-13 17:39:43

标签: ios uiviewcontroller delegates uipickerview uipopover

我在storyboard中创建了一个带有datepicker的popover,当按下按钮时会弹出该弹出窗口。选择日期并按下完成按钮后,日期必须发送到UIViewController

我已按照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;
}

1 个答案:

答案 0 :(得分:1)

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

selectDatePopover.delegate = self;