0

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;
}
Community
  • 1
  • 1
kate
  • 113
  • 1
  • 5
  • 17

1 Answers1

1

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;
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • it returns a error: Property 'delegate' not found on object of type 'UIViewController *' – kate Jul 13 '13 at 21:36
  • 1
    You need to change the UIViewController in this line:" UIViewController *selectDatePopover = [self.storyboard ...." to selectDatePopover (that is your class name -- it would be better for that to be capitalized, but since you didn't, you might want to change the name of the instance so they're not the same). – rdelmar Jul 13 '13 at 22:31
  • the warning is gone..but I still cant get the delegate working :( – kate Jul 13 '13 at 23:00
  • Please update you question to show the code you're trying now. What is not working now? Is _delegate still nil when you log it? – rdelmar Jul 13 '13 at 23:09
  • @KedariTheja, Is this a typo: initWithContentViewController:selectDatePopover. It should now be initWithContentViewController:selectDatePopoverInstance – rdelmar Jul 13 '13 at 23:17