将值从容器视图传递到父视图控制器
Pass values from container view to parent view controller
我是 swift 的新手,我想将一个数组从我的容器视图传递给父级 view.I 已经使用故事板创建了容器视图,但是容器视图以编程方式出现和消失,如图所示以下:
//in parent view controller, inside viewDidLoad()
CameraView.isHidden = true
//in child view controller, on click of button,
let parent = self.parent as! DiaryEntryViewController
parent.CameraView.isHidden = true
我希望在单击按钮显示容器视图时显示所选数据。
我不知道如何将值从子视图控制器传递到父视图控制器,我应该在哪里可以访问函数内需要访问的 values.These 值,该函数在单击父视图内的另一个按钮时调用控制器。
执行此操作的步骤。
你的容器视图必须包含一个嵌入到子视图控制器的转场,将转场命名为这样的东西......"homeToContainer"(见附图)
将此方法添加到您的父视图控制器 (DiaryEntryViewController )
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? ChildViewController,
segue.identifier == "homeToContainer" {
vc.delegate = self
}
}
在子节点中添加协议及其变量ViewController:
protocol ChildToParentProtocol:class {
func buttonClickedByUser()
func needToPassInfoToParent(with value:Int)
}
class ChildViewController: UIViewController {
weak var delegate:ChildToParentProtocol? = nil
@IBAction func createTourPressed(_ sender: UIButton) {
// Call here delegate methods to tell parent about the action
delegate?.buttonClickedByUser()
}
}
在你父 ViewController 的最后,添加这个扩展:
extension DiaryEntryViewController:ChildToParentProtocol {
func buttonClickedByUser() {
}
func needToPassInfoToParent(with value:Int) {
}
}
如果您以编程方式添加容器视图,则应使用回调将值从容器视图返回到父视图。
父控制器:
let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(containerView)
NSLayoutConstraint.activate([
containerView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
containerView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
containerView.topAnchor.constraint(equalTo: headerView.bottomAnchor, constant: 0),
containerView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0),
])
let controller = YourViewController(nibName: "YourNibView", bundle: nil)
//This is important to get value back and also define callback to container view call as well
controller.callback = { result in
self.isCardJoinedFromJoinScreen = true
self.viewWillAppear(true)
}
addChild(controller)
controller.view.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(controller.view)
NSLayoutConstraint.activate([
controller.view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
controller.view.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
controller.view.topAnchor.constraint(equalTo: containerView.topAnchor),
controller.view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
])
容器视图控制器
定义这个回调
var callback : ((Bool)->())?
并且在您将值传递给父控制器时调用此回调,如
self.callback?(true) or self.callback?(false)
我是 swift 的新手,我想将一个数组从我的容器视图传递给父级 view.I 已经使用故事板创建了容器视图,但是容器视图以编程方式出现和消失,如图所示以下:
//in parent view controller, inside viewDidLoad()
CameraView.isHidden = true
//in child view controller, on click of button,
let parent = self.parent as! DiaryEntryViewController
parent.CameraView.isHidden = true
我希望在单击按钮显示容器视图时显示所选数据。 我不知道如何将值从子视图控制器传递到父视图控制器,我应该在哪里可以访问函数内需要访问的 values.These 值,该函数在单击父视图内的另一个按钮时调用控制器。
执行此操作的步骤。
你的容器视图必须包含一个嵌入到子视图控制器的转场,将转场命名为这样的东西......"homeToContainer"(见附图)
将此方法添加到您的父视图控制器 (DiaryEntryViewController )
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if let vc = segue.destination as? ChildViewController, segue.identifier == "homeToContainer" { vc.delegate = self } }
在子节点中添加协议及其变量ViewController:
protocol ChildToParentProtocol:class { func buttonClickedByUser() func needToPassInfoToParent(with value:Int) } class ChildViewController: UIViewController { weak var delegate:ChildToParentProtocol? = nil @IBAction func createTourPressed(_ sender: UIButton) { // Call here delegate methods to tell parent about the action delegate?.buttonClickedByUser() } }
在你父 ViewController 的最后,添加这个扩展:
extension DiaryEntryViewController:ChildToParentProtocol { func buttonClickedByUser() { } func needToPassInfoToParent(with value:Int) { } }
如果您以编程方式添加容器视图,则应使用回调将值从容器视图返回到父视图。
父控制器:
let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(containerView)
NSLayoutConstraint.activate([
containerView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
containerView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
containerView.topAnchor.constraint(equalTo: headerView.bottomAnchor, constant: 0),
containerView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0),
])
let controller = YourViewController(nibName: "YourNibView", bundle: nil)
//This is important to get value back and also define callback to container view call as well
controller.callback = { result in
self.isCardJoinedFromJoinScreen = true
self.viewWillAppear(true)
}
addChild(controller)
controller.view.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(controller.view)
NSLayoutConstraint.activate([
controller.view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
controller.view.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
controller.view.topAnchor.constraint(equalTo: containerView.topAnchor),
controller.view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
])
容器视图控制器
定义这个回调
var callback : ((Bool)->())?
并且在您将值传递给父控制器时调用此回调,如
self.callback?(true) or self.callback?(false)