PopupViewController向ViewController返回null

时间:2013-07-21 22:42:07

标签: ios uipopovercontroller

我想将datepicker.date从DatePickerPopupViewController传递给我的SensorvViewcontroller,如下所示。但是,它返回“null”。我究竟做错了什么?我的代码如下:

#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

2 个答案:

答案 0 :(得分:1)

此代码:

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

应该更像是:

    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];

你应该设置一个委托关系或一个块回调,这样当用户实际选择一个日期时,一个方法被调用,你可以这样做:

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

或者,更好的是,弹出控制器从选择器获取日期并将其作为参数传递给回调方法/块。


类似(键入内联):

在popover中:

typedef void (^CompletionBlock) ();

@propertty (copy, nonatomic) CompletionBlock completionBlock;

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

在源代码管理器中:

self.popoverController.completionBlock = ^{
    NSLog(@"%@", self.popoverController.datePicker.date);
};

答案 1 :(得分:0)

以下代码有效:

//在标题

@property (nonatomic,strong) UIDatePicker *datePicker;

//在实施中

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