0

I would like to pass datepicker.date from DatePickerPopupViewController to my SensorvViewcontroller which is shown below. However, it returns me "null". What am I doing wrong? My code is as follows:

#import "DatePopoverViewController.h"

@interface DatePopoverViewController ()

@end

@implementation DatePopoverViewController
@synthesize datePicker;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    datePicker.date=[NSDate date];
}



@end

#import "SensorViewController.h"

@interface SensorViewController ()

@end
// this represent my button which triggers popupViewController

-(void)chooseDate
{

    if([popoverController isPopoverVisible])
    {
    [popoverController dismissPopoverAnimated:YES];

    }
    else
    {
        CGRect popRect=CGRectMake (900,1,1,1);
        //CGRect popRect=CGRectMake (tools.frame.origin.x,tools.frame.origin.y,50,50);
        popoverController.popoverContentSize =CGSizeMake(320,216);
        [popoverController presentPopoverFromRect:popRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
        datepop=[[DatePopoverViewController alloc]init];
        NSDate *ali= datepop.datePicker.date;
        NSLog(@"%@",ali);
    }

}

   @end

enter image description here

casillas
  • 16,351
  • 19
  • 115
  • 215

2 Answers2

1

This code:

    CGRect popRect=CGRectMake (900,1,1,1);
    //CGRect popRect=CGRectMake (tools.frame.origin.x,tools.frame.origin.y,50,50);
    popoverController.popoverContentSize =CGSizeMake(320,216);
    [popoverController presentPopoverFromRect:popRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    datepop=[[DatePopoverViewController alloc]init];
    NSDate *ali= datepop.datePicker.date;
    NSLog(@"%@",ali);

Should probably be more like:

    popoverController=[[DatePopoverViewController alloc]init];

    CGRect popRect=CGRectMake (900,1,1,1);
    //CGRect popRect=CGRectMake (tools.frame.origin.x,tools.frame.origin.y,50,50);
    popoverController.popoverContentSize =CGSizeMake(320,216);
    [popoverController presentPopoverFromRect:popRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

And you should setup a delegate relationship or a block callback so that when the user has actually selected a date a method is called and you can do:

NSDate *ali= popoverController.datePicker.date;
NSLog(@"%@",ali);

Or, better, the popover controller gets the date from the picker and passes it as a parameter to the callback method / block.


Something like (typed inline):

In the popover:

typedef void (^CompletionBlock) ();

@propertty (copy, nonatomic) CompletionBlock completionBlock;

 - (void)dateSelected {
    self.completion();
}

In the source controller:

self.popoverController.completionBlock = ^{
    NSLog(@"%@", self.popoverController.datePicker.date);
};
Wain
  • 118,658
  • 15
  • 128
  • 151
  • Actually in my viewDidLoad method , I initited already controller =[[DatePopoverViewController alloc] initWithNibName:@"DatePopoverViewController" bundle:nil]; popoverController =[[UIPopoverController alloc]initWithContentViewController:controller]; – casillas Jul 21 '13 at 23:16
  • Ok, so you don't need to create it again, you just need to setup a way for it to handle the date selection and pass it back to your source controller. – Wain Jul 21 '13 at 23:17
  • That is exactly my problem. I have not still able to handle it. I am missing something. your answer clicked accepted answer already. – casillas Jul 21 '13 at 23:20
  • Quickest: add a property to your popover controller which takes a block (make sure it's a `copy` property). When the user chooses the date, call the block. In the block, get the date from the popovers picker. – Wain Jul 21 '13 at 23:24
  • Could you please write a small code in your answer above then I could able to figure out the terms.? – casillas Jul 21 '13 at 23:25
0

This following code works:

//in header

@property (nonatomic,strong) UIDatePicker *datePicker;

//in implementation

UIViewController* popoverContent = [[UIViewController alloc] init]; //ViewController

UIView *popoverView = [[UIView alloc] init];   //view
popoverView.backgroundColor = [UIColor blackColor];

datePicker=[[UIDatePicker alloc]init];//Date picker
datePicker.frame=CGRectMake(0,44,320, 216);
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
[datePicker setMinuteInterval:5];
[datePicker setTag:10];
[datePicker addTarget:self action:@selector(Result) forControlEvents:UIControlEventValueChanged];
[popoverView addSubview:datePicker];

popoverContent.view = popoverView;
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
popoverController.delegate=self;
[popoverContent release];

[popoverController setPopoverContentSize:CGSizeMake(320, 264) animated:NO];
[popoverController presentPopoverFromRect:tempButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];//tempButton.frame where you need you can put that frame

-(void)Result
{

NSLog (@"Pciked Date%@", datePicker.date);
}
casillas
  • 16,351
  • 19
  • 115
  • 215