1.UIPopoverController
The UIPopoverController class is used to manage the presentation of content in a popover. You use popovers to present information temporarily but in a way that does not take over the entire screen like a modal view does. The popover content is layered on top of your existing content in a special type of window. The popover remains visible until the user taps outside of the popover window or you explicitly dismiss it. Popover controllers are for use exclusively on iPad devices. Attempting to create one on other devices results in an exception.
以及上来自Apple API,在API中提到,UIPopoverController只用于iPad,这一点需要注意
2.用法,新建一个工程,选择Empty Application,Product Name为UIPopoverControllerTest,Device选择iPad。
注意,为了简洁不用勾选Use Core Data; Use Automatic Reference Counting; Include Unit Tests这三项
在工程下New File->Object-C class , Subclass of 输入UIViewController, class输入MainViewController, Next,新建MainViewController完成
同理,再建立一个SecondViewController文件
在SecondViewController.m中,将- (void)viewDidLoad改为,设置背景颜色为蓝色是为了方便区分
- (void)viewDidLoad
{
[superviewDidLoad];
[self.viewsetBackgroundColor:[UIColor blueColor]];
}
在MainViewController.h中,定义两个变量@interface MainViewController :UIViewController
{
UIPopoverController * myPopoverControll;
SecondViewController * secondViewController;
}
在MainViewController.m中,初始化这两个变量,并且定义一个可见的按钮tempButtonsecondViewController = [[SecondViewControlleralloc] init];
myPopoverControll = [[UIPopoverControlleralloc] initWithContentViewController:secondViewController];
[secondViewController release];
[myPopoverControllsetPopoverContentSize:CGSizeMake(320,480)];
[myPopoverControll setDelegate:self];
//可见按钮
UIButton * tempButton = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[tempButton setFrame:CGRectMake(300,300, 80, 40)];
[tempButton setTitle:@"Tap"forState:UIControlStateNormal];
[tempButton addTarget:selfaction:@selector(clickButton:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:tempButton];
定义按钮触发的函数:
- (void)clickButton:(UIButton *)sender
{
[myPopoverControllpresentPopoverFromRect:sender.frameinView:self.viewpermittedArrowDirections:UIPopoverArrowDirectionUpanimated:YES];
}
点击Tap按钮,效果如下图:
3.内存解析,如何释放secondViewController所占用的内存?
MainViewController.h中实现UIPopoverControllerDelegate代理。
在- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController中
写上下面这句
[popoverController release];
并且在 myPopoverControll = [[UIPopoverController alloc] initWithContentViewController:secondViewController];后面加上[secondViewController release];
说明,只有popoverController清除时,它上面的Controller才会清除