2

我在使用 swift 让 UIPopover 出现时遇到了一些麻烦。被注释掉的代码在 Objective-C 中运行良好,但在 Swift 中无法运行。当我在视图控制器中点击 + 时,我确实在调试器中获得了“点击”,但是没有出现弹出窗口。

class PlayerInformationTableViewController: UITableViewController, NSFetchedResultsControllerDelegate, UIPopoverControllerDelegate {

    @IBOutlet weak var addBarButtonItem: UIBarButtonItem!

    var playerInformationViewController = PlayerInformationViewController()
    var popover:UIPopoverController?    = nil

    override func viewDidLoad() {
        super.viewDidLoad()

        /*

        //setup the popover
        _cuesPopoverViewController          = [self.storyboard instantiateViewControllerWithIdentifier:@"CuesPopoverViewController"];
        self.cuesPopover                    = [[UIPopoverController alloc] initWithContentViewController:_cuesPopoverViewController];
        self.cuesPopover.popoverContentSize = CGSizeMake(540, 300);
        self.cuesPopover.delegate           = self;

        */

    playerInformationViewController.storyboard?.instantiateViewControllerWithIdentifier("PlayerInformationViewController")
    popover?.contentViewController = playerInformationViewController
    popover?.popoverContentSize = CGSizeMake(300, 300)
    popover?.delegate = self


        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

@IBAction func addPopover(sender: AnyObject) {

    println("Click")

    popover?.presentPopoverFromBarButtonItem(addBarButtonItem, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)

}

解决方案

  override func viewDidLoad() {
        super.viewDidLoad()

     }

@IBAction func addPopover(sender: AnyObject) {

    var popoverViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PlayerInformationViewController") as UIViewController
    popoverViewController.modalPresentationStyle = .Popover
    popoverViewController.preferredContentSize   = CGSizeMake(450, 450)

    let popoverPresentationViewController = popoverViewController.popoverPresentationController

    popoverPresentationViewController?.permittedArrowDirections = .Any
    popoverPresentationViewController?.delegate = self
    popoverPresentationViewController?.barButtonItem = sender as UIBarButtonItem

    presentViewController(popoverViewController, animated: true, completion: nil)

}
4

3 回答 3

6

这是 iOS 8 的一个简单示例。在 iOS 8 中使用自适应 API 呈现 Popover。

class PlayerInformationTableViewController: UITableViewController, UIPopoverPresentationControllerDelegate, NSFetchedResultsControllerDelegate{

   ...


  @IBAction func addPopover(sender: UIBarButtonItem){
    let playerInformationViewController =  PlayerInformationViewController()
    playerInformationViewController.modalPresentationStyle = .Popover
    playerInformationViewController.preferredContentSize = CGSizeMake(300, 300)



    let popoverPresentationViewController = playerInformationViewController.popoverPresentationController
    popoverPresentationViewController?.permittedArrowDirections = .Any
    popoverPresentationViewController?.delegate = self
    popoverPresentationController?.barButtonItem = sender
    presentViewController(playerInformationViewController, animated: true, completion: nil)
  }


  func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!) -> UIModalPresentationStyle{
    return .None
  }

}
于 2014-09-16T13:36:12.360 回答
1

使用来自 xib 的 contentView 显示弹出框

func showPopover(sender: AnyObject) {

    let contentViewController = UINib(nibName: "ContentVC", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as ContentVC
    contentViewController.modalPresentationStyle = UIModalPresentationStyle.Popover

    var detailPopover: UIPopoverPresentationController = contentViewController.popoverPresentationController!
    detailPopover.delegate = self
    detailPopover.barButtonItem = sender as UIBarButtonItem
    detailPopover.permittedArrowDirections = UIPopoverArrowDirection.Any
    presentViewController(contentViewController,
        animated: true, completion:nil)
}

接下来允许在 iPhone 上制作非全屏 PopoverView,不要忘记继承 MainViewController: UIPopoverPresentationControllerDelegate 并将委托设置为 PopoverView

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!) -> UIModalPresentationStyle 
{
    return .None
}
于 2015-04-08T12:00:43.947 回答
0

看起来你popovernil。你在哪里分配/实例化它?

尝试改变这个:

popover?.presentPopoverFromBarButtonItem(addBarButtonItem, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)

对此:

if let pop = popover {
    pop.presentPopoverFromBarButtonItem(addBarButtonItem, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
} else {
    NSLog("Error: Popover was nil")
}

我想您会在控制台中看到该错误消息。在您的.XIB 中PlayerInformationTableViewController,您有一个UIPopoverController吗?

如果是这样,您可能需要确保var popover(1) 在您的 中手动实例化awakeFromNib,或者它是一个@IBOutlet并且正在正确连接。

或者,您可以简单地使用popover您的playerInformationViewController?

于 2014-09-16T13:31:17.060 回答