0

我有一个 UIPopover,我以编程方式创建了几个 UITextField。如果点击其中一个 UITextfields,我希望 UIPopoverController 内的 UIDatePicker 指向导致点击的 UITextfield。我能够得到标签,所以我知道哪个 CGRect 导致了点击;问题是我无法让 UIPopoverController 的箭头指向那个特定的矩形。

这是代码(inView:self.frame 不正确;这只是我尝试过的事情之一)

    if(textField.tag == 1301  ) {  //  'startTime' text field

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

    UIDatePicker *datePicker=[[UIDatePicker alloc]init];//Date picker
    datePicker.frame=CGRectMake(0,44,180,186);  //  180,186
    datePicker.datePickerMode = UIDatePickerModeTime;
    [datePicker setMinuteInterval:15];
    [datePicker setTag:1310];
    [datePicker addTarget:self action:@selector(Result) forControlEvents:UIControlEventValueChanged];
    [popoverView addSubview:datePicker];

    popoverContent.view = popoverView;

    innerPopoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];

    [innerPopoverController setPopoverContentSize:CGSizeMake(180,206) animated:NO];
    [innerPopoverController presentPopoverFromRect:CGRectMake(305, -190, 80, 35)  //  WRONG!  TODO
                                       inView:self.frame
                          permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

}

CGRectMake 具有与创建 UITextfield 相同的参数 (x,y,w,h)。我得到的错误是:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:]:无法从没有窗口的视图中呈现弹出框。”

这甚至可能,在 UIPopover 中有一个 UIPopover 吗?

4

1 回答 1

0

它应该是这样的:

innerPopoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];

[innerPopoverController setPopoverContentSize:CGSizeMake(180,206) animated:NO];
[innerPopoverController presentPopoverFromRect:CGRectMake(305, -190, 80, 35)  //  WRONG!  TODO
                                   inView: SubViewData.view
                      permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

如果您有视图的标签,请执行以下操作:

UIView *tappedView=[SubViewData.view viewWithTag:yourTag];

innerPopoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];

[innerPopoverController setPopoverContentSize:CGSizeMake(180,206) animated:NO];
[innerPopoverController presentPopoverFromRect:CGRectMake(305, -190, 80, 35)  //  WRONG!  TODO
                                       inView: tappedView.superView
                          permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
于 2013-05-14T04:06:21.927 回答