2

I want to show a popup menu and without using nib.I dont like using nib since the headache to implement delegate for a simple functionality. I succeeded using modalPresentationStyle as a Popover to show ViewController as a Popover and it works fine with the below code.

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var btnShowPopOver: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func showPopUP(sender: AnyObject) {

        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let popVC = storyboard.instantiateViewControllerWithIdentifier("pop")as! PopViewController
        popVC.modalPresentationStyle = UIModalPresentationStyle.Popover
        popVC.preferredContentSize = CGSizeMake(320, 240)
        popVC.popoverPresentationController!.delegate = self


        let popOverController = popVC.popoverPresentationController
        popOverController!.sourceView = sender as! UIView // where to stick the bar item in which view
        popOverController!.sourceRect = CGRectMake(70,30, 0, 0) //where to stick the bar

        popOverController?.permittedArrowDirections = nil
        self.presentViewController(popVC, animated: true, completion: nil)


    }




}


extension ViewController :UIPopoverPresentationControllerDelegate {

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


    func popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController) {

        var controller = popoverPresentationController.presentedViewController as! PopViewController
         println(" this is data from pop view controller \(controller.textField.text)")

    }


}

Apple doc says

Popover controllers are for use exclusively on iPad devices. Attempting to create one on other devices results in an exception.

I tested this on real device iphone-6 and its working fine..I am loving this Popover.

Should i use Popover or not as per the apple documentation?Since its working fine in iphone,will my app get rejected for using it later?

1 Answers1

0

Since iOS8 we are now able to create popovers, that will be the same on iPhone, as on iPad, which would be especially awesome for those who make universal apps, thus no need to make separate views or code.

Source : UIPopoverController for iphone not working?

Look at this answers :

https://stackoverflow.com/a/14789022/3202193

https://stackoverflow.com/a/30418212/3202193

In the per-release document of iOS 9 also they are saying like :

The UIPopoverController class is used to manage the presentation of content in a popover. You use popovers to present information temporarily. The popover content is layered on top of your existing content and the background is dimmed automatically. 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.

EDIT

I think you have used the UIPopoverPresentationController which is Available in iOS 8.0 and later.

The UIPopoverController and UIPopoverPresentationController are two different things provided by Apple.

Community
  • 1
  • 1
Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
  • @copeME I hope you have to use iOS>8 . but please test it in iOS 7 simulator or device too. Hope for the best. – Ashish Kakkad Jul 10 '15 at 04:42
  • i dont have that one..and takes time to download in simulator..anyway will figure out soon –  Jul 10 '15 at 04:51
  • @copeME Ok. Let me know :) Because I also like that popover should available in iPhone – Ashish Kakkad Jul 10 '15 at 04:52
  • 1
    @copeME I have updated answer. you are doing perfect. You can show popover in iOS 8 and later through use of `UIPopoverPresentationController` – Ashish Kakkad Jul 10 '15 at 05:02