gpt4 book ai didi

swift - 从另一个 ViewController Swift 调用一个 View

转载 作者:行者123 更新时间:2023-11-28 15:26:07 25 4
gpt4 key购买 nike

我有一个 ViewController,它上面设置了一个 UIView,还有一个按钮可以打开另一个 ViewController 的弹出框。我想要弹出 View Controller 上的一个按钮来将 UIView 设置为禁用。如何从第二个 View Controller 中的按钮引用第一个 View Controller 中的 UIView?

enter image description here

编辑:

下面是我用来调用弹出 View Controller 的代码。请注意我是如何从第一个 View Controller 调用 dimView.isHidden = false 的。我想从弹出 View Controller 运行 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:

下面是我的弹出 View Controller 。由于它不叫 PopoverVC。我更新了答案以包括 let popover = storyboard?.instantiateViewController(withIdentifier: "PopoverVC") 作为! 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!

最佳答案

您可以在呈现您的 PopoverVC 时传递对当前 View Controller 的引用,然后您可以从 PopoverVC 访问它的 View 。只需在 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)

弹出 View Controller :

class PopOverViewController: UIViewController {

var dimView:UIView?

override func viewDidLoad() {
super.viewDidLoad()
}

@IBAction func closeButton(_ sender: Any) {
self.dismiss(animated: false, completion: nil)
}

}

关于swift - 从另一个 ViewController Swift 调用一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45193417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com