0

我想将 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

在此处输入图像描述

4

2 回答 2

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

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


类似(内联输入):

在弹出窗口中:

typedef void (^CompletionBlock) ();

@propertty (copy, nonatomic) CompletionBlock completionBlock;

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

在源控制器中:

self.popoverController.completionBlock = ^{
    NSLog(@"%@", self.popoverController.datePicker.date);
};
于 2013-07-21T23:01:55.947 回答
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);
}
于 2013-07-22T00:09:09.707 回答