【问题标题】:Swift : Dismiss a PopOver when a tableview cell is selectedSwift:选择表格视图单元格时关闭 PopOver
【发布时间】:2015-07-13 13:31:50
【问题描述】:

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

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

我希望能够选择一个单元格,然后关闭弹出视图。

另一个奇怪的行为是第一个单元格显示一个警报视图控制器,它在 iPhone 上运行良好,但在 iPad 上,弹出框突然调整为警报视图控制器的大小。

这是我的主视图控制器:

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
}


}

这是我的弹出框:

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



}

谢谢。

【问题讨论】:

    标签: swift popover dismiss


    【解决方案1】:

    self.dismissViewControllerAnimated(true, completion: nil)

    在 menuViewController 中足以消除弹出框。所以代码将如下所示:

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

    【讨论】:

    • 我需要在显示警报控制器之前关闭它,而不是在单击取消按钮时
    • 好的,然后将 self.dismissViewControllerAnimated(true, completion: nil) 放在 cancelAction 的块之外。我已经像这样编辑了我的答案。
    • @KorayBirand 你有没有办法把选择的数据带回主视图控制器?
    • 我正在尝试做同样的事情,但不是警报,而是使用 ViewController。问题是当我按下一个项目时,viewController 出现然后消失,但弹出框仍然可见/:有人帮助我吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 2020-09-11
    • 2016-05-01
    • 1970-01-01
    相关资源
    最近更新 更多