gpt4 book ai didi

ios - PopupViewController 将 null 返回给 ViewController

转载 作者:行者123 更新时间:2023-11-29 03:45:48 26 4
gpt4 key购买 nike

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

最佳答案

这段代码:

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

并且您应该设置委托(delegate)关系或 block 回调,以便当用户实际选择日期时调用方法,您可以执行以下操作:

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

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


类似于(内联键入):

在弹出窗口中:

typedef void (^CompletionBlock) ();

@propertty (copy, nonatomic) CompletionBlock completionBlock;

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

在源 Controller 中:

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

关于ios - PopupViewController 将 null 返回给 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17777689/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com