1

I am trying to insert a uipopover with uidatepicker in it with two buttons - Cancel and Done. I was able to design the layout using storyboard with the help of UIDatePicker in UIPopover and How can I show a UIDatePicker inside a Popover on iPad using StoryBoard?

But, I am not able to get the date from uidatepicker to uiviewcontroller when Done button is pressed. I am new to ios programming and i find delegate methods very confusing. Could somebody shed some light? Help much appreciated. Thank you :)

enter image description here

Community
  • 1
  • 1
kate
  • 113
  • 1
  • 5
  • 17

2 Answers2

3

You are absolutely correct one way to get the value in view controller is to implement delegate functions. These are very easy if you understand at the technical level. I will try to explain it here.

you have to define the protocol in the datePickerViewcontrollerClass.h like this

@protocol TimePopupViewControllerDelegate <NSObject> 
-(void)returnSelectedDate:(NSDate*)date;
@end

and create a instance of 'id' type for passing the reference of mainViewController like this.

@property (nonatomic, assign) id < TimePopupViewControllerDelegate > delegate;

in MainViewController.m where you are creating instance of datePickerViewcontrollerClass, you have to set the delegate like this

datePickerViewcontrollerClass *myViewControllerForPopover =[[datePickerViewcontrollerClass alloc] init];
myViewControllerForPopover.delegate = self;

in the method where you getting the date from picker in datePickerViewcontrollerClass.m class you have to pass it to main class using delegate.

-(void)viewDidDisappear:(BOOL)animated{
     [_delegate returnSelectedDate:datepicker.date];
[super viewWillDisappear:animated];

}

you can write this in any method I have written in ViewWillDisappear or any other method.

After this in MainViewController this method get called and you can retrieve the selected date

-(void)returnSelectedDate:(NSDate *)date{
}

Technically you are passing the referece of mainViewController instance to your datePickerViewcontrollerClass and calling the methods on mainViewController from datePickerViewcontrollerClass I hope I will be able to explain it clearly if you still any doubt you can comment.

Manish Agrawal
  • 10,958
  • 6
  • 44
  • 76
  • Thanks @Parser, for the very detailed explaination. I have done like you said except that I called the delegate method inside donebutton press..the delegate doesnt seem to be working..also how can I dismiss the popover on the click of done/cancel button? – kate Jul 13 '13 at 15:57
  • Also, DO I need to perform any segues?? – kate Jul 13 '13 at 16:30
1

In UIPopover, you must be loading one viewcontroller which contains that datepicker.

So in that viewcontroller, write IBAction methods for Done and Cancel buttons.

Done button:

  • We can achieve this using Protocol -

DatePickerContainerVC.delegate = myViewController

  • Requires to declare a method in Protocol -(void)selectedDate: (NSDate *)aDate and implement this method in myViewController
  • Call a delegate method from DateContainerVC - [self.delegate selectedDate:datePicker.date];

  • This will call your view controller method where you can change your label or button text with date parameter.

Cancel button:

  • Call delegate method same as above -(void)cancelDatePickerPopover;
  • Here dismiss your popover controller
Mrunal
  • 13,982
  • 6
  • 52
  • 96
  • Can you be more detail..inside the action method of done button, I am calling the delegate method, it doesnt seem to be working :( '- (IBAction)doneButtonPress:(UIBarButtonItem *)sender { NSLog(@"donebuttonpress"); [self.delegate receiveDate:pickedDate]; }' – kate Jul 13 '13 at 15:46