gpt4 book ai didi

ios - 容器 View 中的数据仅传递给子 VC 一次

转载 作者:行者123 更新时间:2023-11-29 01:20:50 27 4
gpt4 key购买 nike

我正在尝试将数据传递到容器 View ,该 View 将根据用户状态包含多个 subview Controller 中的任何一个。我在第一次实例化新 VC 时成功传递了数据,但是如果我关闭并重新打开那个子 VC,数据就不会再次传递。这是按下按钮时的操作:

@IBAction func onMapButtonPressed(sender: UIButton) {
let latitude = Double(selectedPlace.latitude!)
let longitude = Double(selectedPlace.longitude!)
bringInSubview("mapViewScreen")
(childViewControllers[0] as! PlaceMapDetailVC).latitude = latitude!
(childViewControllers[0] as! PlaceMapDetailVC).longitude = longitude!
(childViewControllers[0] as! PlaceMapDetailVC).placeName = selectedPlace["name"] as! String
}

以及辅助方法:

func bringInSubview(name:String) {

newViewController = self.storyboard?.instantiateViewControllerWithIdentifier(name)
newViewController!.view.translatesAutoresizingMaskIntoConstraints = false
self.addChildViewController(self.newViewController!)
self.addSubview(self.newViewController!.view, toView: self.containerView)
view.bringSubviewToFront(containerView)
view.bringSubviewToFront(closeSubviewButton)

UIView.animateWithDuration(0.3, animations: {
self.containerView.alpha = 1
}, completion: { finished in
// anything else needed in completion block?
})
}

func addSubview(subView:UIView, toView parentView:UIView) {
parentView.addSubview(subView)
var viewBindingsDict = [String: AnyObject]()
viewBindingsDict["subView"] = subView
parentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subView]|",
options: [], metrics: nil, views: viewBindingsDict))
parentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subView]|",
options: [], metrics: nil, views: viewBindingsDict))
}

最后摆脱了 child VC:

@IBAction func onCloseButtonInSubviewPressed(sender: UIButton) {
UIView.animateWithDuration(0.3, animations: {
self.containerView.alpha = 0
self.view.sendSubviewToBack(self.closeSubviewButton)
},
completion: { finished in
self.view.sendSubviewToBack(self.containerView)

// clears out prior view that was there to free up memory
for view in self.containerView.subviews {
view.removeFromSuperview()
}
})
}

非常感谢任何见解,谢谢!!!

最佳答案

我发现你的代码有问题。在 func onMapButtonPressed(sender: UIButton) 中,您假设位置始终为 0。它在第一次工作是因为它在那里。但是在您第一次删除它并在 bringInSubview(name:String) 中重新添加后,位置发生了变化。你应该检查一下。

希望对您有所帮助!

关于ios - 容器 View 中的数据仅传递给子 VC 一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34620734/

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