Question

I have created a popover with datepicker in storyboard which pops when a button is pressed. After a date is selected and done button is pressed, the date has to be sent to the UIViewController.

I have set up protocols and delegate methods as explained by uidatepicker in uipopover delegate but the delegate method in my viewcontroller is never called up. Am I missing something? Do I need to use segues for this?? Please help!!

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

Both the donebuttonpress and afterdelegation are printed, but the method has never been called :(

//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;
}

//On the press on this button, popover has to show up:

- (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;
}
Was it helpful?

Solution

This line: self.popover.delegate = self; is incorrect. The delegate protocol isn't in the popover class, it's in its content view controller, which is selectDatePopover. So, change that line to:

selectDatePopover.delegate = self;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top