UIPopoverController, Xcode 6, IOS 8 using Swift

孤者浪人 提交于 2019-12-20 10:58:31

问题


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 my view controller I do get the "click" in my debugger, however no popover appears.

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)

}

Solution

  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)

}

回答1:


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
  }

}



回答2:


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
}



回答3:


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?



来源:https://stackoverflow.com/questions/25868508/uipopovercontroller-xcode-6-ios-8-using-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!