0

i am using a uidatepicker inside a uipopover in many viewcontrollers. I need to keep the picked date in each of these viewcontrollers different so I can load them back when uidatepicker is launched again in their respective VC's. How do I accomplish this?

how to send data from uiViewController to uipopover so that the datepicker is set to corresponding VC's intial date. Help much appreciated.

VC code which launches uipopover designed in storyboard:

- (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;
    _popover.delegate =self;
}

code in uipopover:

- (void)viewDidLoad
{
    NSLog(@"viewdidload");
    NSUserDefaults *datepicks = [NSUserDefaults standardUserDefaults];
    NSString *myLastDate = [datepicks objectForKey:@"myLastDate"];

    if (myLastDate == nil) {
        [self.mydatepicker setDate:[NSDate date] animated:NO];
    } else {
         NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
         [formatter setDateStyle : NSDateFormatterLongStyle];

         [self.mydatepicker setDate:[formatter dateFromString:myLastDate] animated:NO];
    }
    [super viewDidLoad];
    NSLog(@"insideview");
// Do any additional setup after loading the view.
}
kate
  • 113
  • 1
  • 5
  • 17

2 Answers2

1

Creating a subclass of UIPopoverController which has a property "data", then pass your data to the class in the constructor. Will this work for you?

kvh
  • 2,118
  • 19
  • 29
0

I was able to solve this by creating a property and setting this in the VC. :) I am guessing, it could also be solved by giving different tags to different instances of uipopover. But I still got to test it. One amazing tutorial that helped me was: http://www.colejoplin.com/2012/09/12/ios-tutorial-combining-delegation-storyboards-popovers-and-data/

Hope that helps somebody somewhere :)

kate
  • 113
  • 1
  • 5
  • 17