UIPopoverController 弃用

UIPopoverController deprecated

我在使用 UIPopoverController 时收到警告,阅读 apple 文档后我了解到这已被弃用,我们必须使用 UIPopoverPresentationController。请任何人帮我替换下面的代码。

UIPopoverController *popover = [[UIPopoverController alloc]   initWithContentViewController:viewDownloader];
[popover setPopoverContentSize: CGSizeMake(320, 450)];

另一种方法

if (popover!=nil && popover.popoverVisible == YES)
    [popover dismissPopoverAnimated:YES];

我需要替换这些代码,但找不到任何等效代码。任何帮助表示赞赏。提前致谢。

UIModalPresentationPopover

UIModalPresentationPopoverUIPopoverController.

的替换

在 iOS 8.0 及更高版本中可用。

ModalViewController *modal = [[ModalViewController alloc] init];
modal.modalPresentationStyle = UIModalPresentationPopover;
modal.transitioningDelegate = self;
modal.popoverPresentationController.sourceView = self.view;
modal.popoverPresentationController.sourceRect = CGRectZero;
modal.popoverPresentationController.delegate = self;

[self presentViewController:modal animated:YES completion:nil];

否则你可以使用下面的link。

UIPopoverPresentationController

下面的代码显示 UIModalPresentationPopover 从选定的 UITableViewCell 中点击的菜单按钮;

-(void)menuButtonTapped:(UIButton *)button {

    UITableViewCell *cell=(UITableViewCell *)[[button superview] superview];
    self.selectedIndexPath = [self.tableView indexPathForCell:cell];
    CGRect rectOfCellInTableView = [self.tableView rectForRowAtIndexPath: self.selectedIndexPath];
    CGRect rectOfCellInSuperview = [self.tableView convertRect: rectOfCellInTableView toView:_tableView.superview];
    rectOfCellInSuperview.origin.x = self.view.frame.size.width-50;

    MyMenuPopoverController *myMenuPopoverController= [[MyMenuPopoverController alloc] initWithStyle:UITableViewStylePlain];

    myMenuPopoverController.modalPresentationStyle = UIModalPresentationPopover;
    myMenuPopoverController.popoverPresentationController.sourceView = self.view;
    myMenuPopoverController.popoverPresentationController.sourceRect = rectOfCellInSuperview;
    myMenuPopoverController.preferredContentSize = CGSizeMake(250,(myMenuPopoverController.arrMenuOptions.count * 40));
}