如何将UIPopover附加到我绘制的矩形

时间:2013-05-01 16:16:43

标签: ios objective-c uipopovercontroller

我有一个iPad应用程序(XCode 4.6,ios 6.2,ARC和Storyboards)。我已经在UIView的位置上绘制了一个GCRect。

    CGRect rectangle = CGRectMake( [appSelected.aPosX floatValue], [appSelected.aPosY floatValue],[appSelected.aPosW floatValue], [appSelected.aPosH floatValue]);

我有一种方法可以为我画画;这是该方法的代码:

-(void)showHTMLHelp:(NSString *)htmlString pointTo:(id)target background:(UIColor *)bgColor  {

UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)];
popoverView.backgroundColor = [UIColor colorWithWhite:(CGFloat)1.0 alpha:(CGFloat)1.0];  //  frame color?
popoverContent.view = popoverView;

//resize the popover view shown in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(200, 300);

//  add the UIWebView for RichText
UIWebView *webView = [[UIWebView alloc] initWithFrame:popoverView.frame];
webView.backgroundColor = [UIColor whiteColor];  //  change background color here

//  add the webView to the popover
[webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:nil]];
[popoverView addSubview:webView];

//create a popover controller
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];

//present the popover view non-modal with a refrence to the button pressed within the current view
if([target isKindOfClass: [UITextField class]])
    [popoverController presentPopoverFromRect:((UITextField *)target).frame inView:self.view
                     permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
else if([target isKindOfClass: [UISegmentedControl class]])
    [popoverController presentPopoverFromRect:((UISegmentedControl *)target).frame inView:self.view
                     permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
else if([target isKindOfClass: [UIButton class]])
    [popoverController presentPopoverFromRect:((UIButton *)target).frame inView:self.view
                     permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
else 
    [popoverController presentPopoverFromRect:*(CGRect *)CFBridgingRetain(target)
                                       inView:self.view
                     permittedArrowDirections:UIPopoverArrowDirectionAny
                                     animated:YES];

}

现在我希望有一个UIPopover指向该矩形。由于GCRect是一个C结构,我无法弄清楚如何做到这一点。这就是我尝试过的,但显然这是错的。我怎样才能做到这一点?这是我调用方法来显示popover的代码:

    PreferencesViewController *pvc = [[PreferencesViewController alloc] init];
[pvc showHTMLHelp:html pointTo: rectangle background:[UIColor whiteColor]];

2 个答案:

答案 0 :(得分:1)

将C结构(或C原始变量)传递给Objective-C方法绝对没有问题。事实上,presentPopoverFromRect:inView:permittedArrowDirections:animated:采用简单的CGRect,正如您可以通过标题(或文档)告诉的那样:

- (void)presentPopoverFromRect:(CGRect)rect 
                        inView:(UIView *)view 
      permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections
                      animated:(BOOL)animated;

简单地称之为:

[popoverController presentPopoverFromRect:rectangle
                                   inView:self.view
                 permittedArrowDirections:UIPopoverArrowDirectionAny
                                 animated:YES];

正如Mar0ux已经指出的那样,它是CGRect,而不是GCRectCG代表Core Graphics

答案 1 :(得分:0)

请问您为什么要发送CGRect并将其转换为“id”?感觉在哪里?!我更愿意将参数类型更改为CGRect。

亲切的问候。

相关问题