Search code examples
iosuipopovercontroller

PopupViewController returns null to ViewController


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


Solution

  • 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);
    };