Swift:选择tableview单元格时关闭PopOver

时间:2015-05-04 08:11:02

标签: swift popover dismiss

我有两个控制器。第一个有一个带有书签按钮项目的导航栏,我按下它来显示我的第二个控制器,里面有一个桌面视图。

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

我希望能够选择一个单元格然后关闭弹出视图。 The layout is as seems

另一个奇怪的行为是第一个单元格显示一个警报视图控制器,它可以在iPhone上正常工作但在iPad上突然显示大小调整到alerviewcontroller的大小。

enter image description here

这是我的主视图控制器:

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
}


}

这是我的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()
}



}

感谢。

1 个答案:

答案 0 :(得分:2)

self.dismissViewControllerAnimated(true,completion:nil)

menuViewController中的

足以解除popover。所以代码看起来像这样:

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