@IBAction func txtPasswordClicked(_ sender: Any) { let popupKeyboardVC = storyboard?.instantiateViewController(withIdentifier: "popupKeyboard") as! PopupKeyboardVC popupKeyboardVC.delegate = self //popupKeyboardVC.strSaveText=labelText.text popupKeyboardVC.modalPresentationStyle = .popover if let popoverController = popupKeyboardVC.popoverPresentationController { popoverController.sourceView = sender as! UIView popoverController.sourceRect = (sender as AnyObject).bounds popoverController.permittedArrowDirections = .any popoverController.delegate = self } present(popupKeyboardVC,animated: true,completion: nil) } func saveText(strText: Nsstring) { txtPassword.text = strText as String; } // MARK: - UIPopoverPresentationControllerDelegate func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!) -> UIModalPresentationStyle { return .fullScreen } func presentationController(controller: UIPresentationController!,viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController! { return UINavigationController(rootViewController: controller.presentedViewController) }
弹出的controller:
protocol SavingViewControllerDelegate { func saveText( strText : Nsstring) } class PopupKeyboardVC: UIViewController,UITextFieldDelegate{ override func viewDidLoad() { } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // dispose of any resources that can be recreated. } @IBOutlet weak var btnDone: UIButton! var delegate : SavingViewControllerDelegate? @IBAction func btnDoneClick(_ sender: Any) { if (self.delegate) != nil { delegate?.saveText(strText: "abc") } self.dismiss(animated: true,completion: nil) } }