UIPopoverController, Xcode 6, IOS 8 using Swift

前端 未结 3 1640
粉色の甜心
粉色の甜心 2021-02-04 22:13

I\'m having some trouble getting a UIPopover to appear using swift. The code that is commented out works fine in Objective-C, but doesn\'t work using Swift. When I tap the + in

相关标签:
3条回答
  • 2021-02-04 22:43

    Display Popover with contentView from xib

    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)
    }
    

    Next allows to make not full screen PopoverView on iPhone for this do not forget to inherit MainViewController: UIPopoverPresentationControllerDelegate and set delegate to PopoverView

    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!) -> UIModalPresentationStyle 
    {
        return .None
    }
    
    0 讨论(0)
  • 2021-02-04 22:50

    Here is a simple example for iOS 8. Popover are presented using adaptivity apis in iOS 8.

    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
      }
    
    }
    
    0 讨论(0)
  • 2021-02-04 23:06

    It looks like your popover is nil. Where are you assigning/instantiating it?

    Try changing this:

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

    To this:

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

    I imagine you'll see that error message in your console. In the .XIB for your PlayerInformationTableViewController, do you have a UIPopoverController?

    If so, you probably need to ensure that the var popover is either (1) being manually instantiated in your awakeFromNib, or that it's an @IBOutlet and is being connected properly.

    Alternatively, can you simply use the popover already present in your playerInformationViewController?

    0 讨论(0)
提交回复
热议问题