ios UIPopoverController 用法、内存解析

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,这一点需要注意

Demo地址


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中,初始化这两个变量,并且定义一个可见的按钮tempButton

    secondViewController = [[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 allocinitWithContentViewController:secondViewController];后面加上[secondViewController release];

说明,只有popoverController清除时,它上面的Controller才会清除

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值