我有一个ViewController,它上面有一个UIView设置,还有一个按钮可以打开另一个ViewController的popover。我想在弹出窗口控制器上设置一个按钮,将UIView设置为禁用。如何从第一个视图控制器中的第二个视图控制器中的按钮引用UIView?
修改
下面是我用来调用popover视图控制器的代码。请注意我是如何从第一个viewcontroller调用dimView.isHidden = false
的。我想从popover视图控制器运行dimView.isHidden = true
。
let popover = storyboard?.instantiateViewController(withIdentifier: "PopoverVC")
popover?.modalPresentationStyle = .popover
popover?.popoverPresentationController?.delegate = self as? UIPopoverPresentationControllerDelegate
popover?.popoverPresentationController?.sourceView = self.view
popover?.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
popover?.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
dimView.isHidden = false
self.present(popover!, animated: false)
编辑2:
下面是我的popover视图控制器。因为它不叫PopoverVC。我更新了答案以包含let popover = storyboard?.instantiateViewController(withIdentifier: "PopoverVC") as! PopOverViewController
,但仍然没有运气。
import UIKit
var parentController:UIViewController?
class PopOverViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func closeButton(_ sender: Any) {
self.dismiss(animated: false, completion: nil)
}
}
编辑3:
class ViewController: FormViewController {
override func viewWillAppear(_ animated: Bool) {
dimView.isHidden = true
}
@IBOutlet weak var dimView: UIView!
答案 0 :(得分:1)
您可以在展示PopoverVC
时向当前视图控制器传递引用,然后您可以从PopoverVC
访问其视图。只需在PopoverVC
中创建一个可以存储引用的属性,例如var parentController:UIViewController?
let popover = storyboard?.instantiateViewController(withIdentifier: "PopoverVC") as! PopoverViewController
popover?.modalPresentationStyle = .popover
popover?.popoverPresentationController?.delegate = self as? UIPopoverPresentationControllerDelegate
popover?.popoverPresentationController?.sourceView = self.view
popover?.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
popover?.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
popover?.dimView = self.dimView
dimView.isHidden = false
self.present(popover!, animated: false)
PopOverViewController:
class PopOverViewController: UIViewController {
var dimView:UIView?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func closeButton(_ sender: Any) {
self.dismiss(animated: false, completion: nil)
}
}