Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift : Dismiss a PopOver when a tableview cell is selected

I have a two controllers. The first one has a navigation bar with an item of Bookmarks Button which I press to display my second controller with has a tableview inside of it.

Example project from this link : http://www.koraybirand.co.uk/download/xcode/PopTest.zip

I want to be able to select one cell and then dismiss the popover view. The layout is as seems

One other strange behaviour is that first cell displays an alert viewcontroller which works fine on an iPhone but on an iPad the popover is suddenly resize to alerviewcontroller's size.

enter image description here

here is my main view controller :

class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {

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

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "popoverSegue" {

        let popoverViewController = segue.destinationViewController as! UIViewController
        popoverViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
        popoverViewController.popoverPresentationController!.delegate = self
    }
}

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


}

and here is my popover:

class menuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var links : [String] = ["Share", "Manage"]
var currentPopover:UIPopoverController!

@IBOutlet weak var tableView: UITableView!

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return links.count
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    if indexPath.item == 0 {
        let alertController = UIAlertController(title: "Share", message: "No Bookmarks to Share", preferredStyle: .Alert)
        let cancelAction = UIAlertAction(title: "Dismiss", style: .Cancel) { (_) in }
        alertController.addAction(cancelAction)
        self.presentViewController(alertController, animated: true) {}
    }
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell

    cell.textLabel?.text = links[indexPath.row] as String
    return cell
}

override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}



}

Thanks.

like image 264
Koray Birand Avatar asked Nov 01 '22 05:11

Koray Birand


1 Answers

self.dismissViewControllerAnimated(true, completion: nil)

in menuViewController is enough to dismiss popover. So code will look like this:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.item == 0 {
        let alertController = UIAlertController(title: "Share", message: "No Bookmarks to Share", preferredStyle: .Alert)
        let cancelAction = UIAlertAction(title: "Dismiss", style: .Cancel) { (_) in }
        alertController.addAction(cancelAction)
        self.presentViewController(alertController, animated: true) {}

        self.dismissViewControllerAnimated(true, completion: nil)
    }
}
like image 125
dadalar Avatar answered Nov 08 '22 12:11

dadalar