以编程方式设置UIPopOver视图的标题

时间:2010-07-21 06:08:47

标签: iphone objective-c

如何以编程方式设置UIPopOver View的标题?

我找到了一些示例代码,但无法设置标题。

myView *theView = [[myView alloc] initWithNibName:@"myView" 
                                       bundle:nil];
UIPopoverController* aPopover = [[UIPopoverController alloc] initWithContentViewController:theView];
[aPopover setDelegate:self];
[aPopover setPopoverContentSize:CGSizeMake(320, 320) animated:YES];

[theView setPopover:aPopover];
[theView release];

[self.popoverController presentPopoverFromRect:CGRectMake(510,370,0,0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

4 个答案:

答案 0 :(得分:7)

您需要将视图控制器包装在UINavigationCotnroller中,这将为视图控制器添加一个带有相应标题的导航栏。像这样:

UINavigationController *container =
    [[[UINavigationController alloc] initWithRootViewController:viewController] autorelease];

然后只需初始化您的popover以使用container代替并按常规显示它。

答案 1 :(得分:4)

是的,确切地说。整件事情看起来像这样:

InfoView *infoView = [[InfoView alloc] init];
UINavigationController *container = [[[UINavigationController alloc] initWithRootViewController:infoView] autorelease];
UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:container];

infoView.title = @"My Title";

[pop setDelegate:self];
[pop setPopoverContentSize:CGSizeMake(320, 400)];
[pop presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[infoView release];

答案 2 :(得分:1)

let popoverContent = (self.storyboard?.instantiateViewControllerWithIdentifier("Popover"))! as UIViewController
popoverContent.title = "Details"
let nav = UINavigationController(rootViewController: popoverContent)
nav.modalPresentationStyle = UIModalPresentationStyle.Popover
let popover = nav.popoverPresentationController
popoverContent.preferredContentSize = CGSizeMake(100, 100)
popover!.delegate = self
popover!.sourceView = self.view
popover!.sourceRect = CGRectMake(100,100,0,0)

self.presentViewController(nav, animated: true, completion: nil)

答案 3 :(得分:0)

尝试设置popOver的contentViewController的标题:

theView.title = @"My Title";

theView.navigationItem.title = @“我的标题”;