单击UIPopoverPresentationController内的按钮,移动到视图控制器

时间:2017-07-06 06:19:13

标签: ios swift segue uipopovercontroller uipopover

我有一个表视图控制器,其中一个单元格有一个按钮。

单击此按钮,我将视图控制器显示为UIPopoverPresentationController。

此弹出控制器有两个按钮,YES和NO。

点击“是”,我希望能够转到另一个视图控制器,点击“否”,我将关闭弹出控制器。

点击“否”,弹出窗口被取消,但点击“是”,我不会移动到所需的视图控制器。

虽然视图确实调用了该视图控制器的load()方法。问题是没有显示新的视图控制器。

单击“是”,弹出窗口将被取消,但仍保留在上一个视图控制器

以下是我的代码

TableViewController类

func onTapPickButton(sender: UIButton) { 

 let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
        let popoverViewController = storyboard.instantiateViewController(withIdentifier: "ConfirmDialogViewController") as! ConfirmDialogViewController 
        popoverViewController.preferredContentSize = CGSize(width: view.frame.width/1.33, height: view.frame.height/2.34) 
        popoverViewController.modalPresentationStyle = UIModalPresentationStyle.popover 
        let popover: UIPopoverPresentationController = popoverViewController.popoverPresentationController! 
        popover.sourceView = self.view 
        popover.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)         
        popover.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0) 
        popover.delegate = self 

        present(popoverViewController, animated: true, completion: nil) 
}

PopOver View控制器类

@IBAction func onTapNoButton(_ sender: UIButton) { 
        self.dismiss(animated: true, completion: {}) 
    } 

    @IBAction func onTapYesButton(_ sender: UIButton) { 
        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let viewController = storyboard.instantiateViewController(withIdentifier: "View") as! ViewController
present(viewController, animated: true, completion: nil)

    } 

任何帮助将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:0)

在YES按钮操作中,关闭popOver并在完成块中显示新的viewController

 @IBAction func onTapYesButton(_ sender: UIButton) { 
                let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                let viewController = storyboard.instantiateViewController(withIdentifier: "View") as! ViewController
                self.dismiss(animated: true, completion: {
                       self.present(viewController, animated: true, completion: nil)
                }) 

            } 

答案 1 :(得分:0)

您可以创建一个闭包(完成处理程序)或委托来捕获“是”和“#”。按钮被选中。如果您想要从Popover ViewController捕获任何数据,这些将非常有用。例如:

完成处理程序

class ConfirmDialogViewController: UIViewController {
    open var didSelectYesHandler: ((_ selectedData: String) -> ())?

    public init(for sender: UIView, didSelectYesHandler: ((_ selectedData: String) -> ())? = nil) {
        super.init(nibName: nil, bundle: nil)

        modalPresentationStyle = .popover
        self.sender = sender
        self.didSelectDataHandler = didSelectDataHandler

        guard let popoverPC = popoverPresentationController else { return }
        popoverPC.sourceView = sender
        popoverPC.sourceRect = sender.bounds
        popoverPC.delegate = self
    }

    @IBAction func onTapYesButton(_ sender: UIButton) { 
        dismiss(animated: true, completion: nil)
        didSelectDataHandler?("YES")
    }

    // MARK: - PopoverPC Delegate Methods

    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.none
    }
}

<强>代表

protocol ConfirmDialogViewControllerDelegate: class {
    func didSelectYes(_ selectedDate: String)
}

class ConfirmDialogViewController: UIViewController {
    weak var delegate: ConfirmDialogViewControllerDelegate?

    @IBAction func onTapYesButton(_ sender: UIButton) { 
        dismiss(animated: true, completion: nil)
        delegate?.didSelectData("YES")
    } 
}

// In your current view controller set 
popoverViewController.delegate = self

<强>加成

如果您想要一个包含数据列表供选择的弹出窗口,请查看此library