我目前有一个包含2个容器视图的Viewcontroller。第一个用作导航,并水平滚动以显示其他按钮(如材料设计中的底座或滑动标签)。第二是内容。如何通过第一个控制第二个Container视图中显示的内容? 这是一张更能说明我所说的内容的图片。
*注意我只是想更新视图的紫色部分。导航容器(蓝色)上方的所有内容都是静态的。
我已经调查了这个tutorial,如果我搞清楚了,我会更新。
答案 0 :(得分:2)
我建议使用授权。
按下“导航栏”中的所述按钮后,让它告诉控制器更改第二个容器视图的内容。
<强>更新强>:
在导航中添加协议:
protocol NavigationDelegation: class {
func userDidPressSomeButton(sender: NavigationViewController)
}
class NavigationViewController: UIViewController {
...
weak var navDelegate: NavigationDelegation?
@IBAction func someButton(_ sender: Any) {
// some additional stuff
...
// send delegation
navDelegate?.userDidPressSomeButton(sender: self)
}
...
}
在主控制器内部,遵守协议并实施委派:
class YourMainViewController: UIViewController, NavigationDelegation {
...
var firstEmbeddedViewController: NavigationViewController?
var secondEmbeddedViewController: SomeOtherViewController?
// Accessing your embedded controllers
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let controller = segue.destination as? NavigationViewController {
embeddedViewController = controller
// Setting your main controller to delegate to
embeddedViewController!.navDelegate = self
}
else if let controller = segue.destination as? SomeOtherViewController {
secondEmbeddedViewController = controller
}
}
// Implement the required protocol function
func userDidPressSomeButton(sender: NavigationViewController) {
// You can now talk to your second embedded view, e.g.,
secondEmbeddedViewController.someVariable = someValue
secondEmbeddedViewController.someFunction()
// You can also use the sender variable to access your navigation properties, e.g.,
// let someText = sender.buttonOutlet.textLabel?.text
}
...
}
答案 1 :(得分:0)
完全赞同雷。授权是最好和最灵活的解决方案。 我有几乎相同的问题,但有主人和详细的观点。我的解决方案是How to update DetailView
答案 2 :(得分:0)
主要VC:
var container: ContainerViewController!
override func viewDidLoad() {
container!.segueIdentifierReceivedFromParent("first")
}
@IBAction func firstBtnPressed(_ sender: Any) {
let vc = "first"
container!.segueIdentifierReceivedFromParent(vc)
}
@IBAction func secondBtnPressed(_ sender: Any) {
container!.segueIdentifierReceivedFromParent("second")
}
@IBAction func thirdBtnPressed(_ sender: Any) {
container!.segueIdentifierReceivedFromParent("third")
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "container"{
container = segue.destination as! ContainerViewController
}
}
Conatiner View
open class ContainerViewController: UIViewController {
//Manipulating container views
fileprivate weak var viewController : UIViewController!
//Keeping track of containerViews
fileprivate var containerViewObjects = Dictionary<String,UIViewController>()
/** Specifies which ever container view is on the front */
open var currentViewController : UIViewController{
get {
return self.viewController
}
}
fileprivate var segueIdentifier : String!
/*Identifier For First Container SubView*/
@IBInspectable internal var firstLinkedSubView : String!
override open func viewDidLoad() {
super.viewDidLoad()
}
open override func viewDidAppear(_ animated: Bool) {
if let identifier = firstLinkedSubView{
segueIdentifierReceivedFromParent(identifier)
}
}
override open func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func segueIdentifierReceivedFromParent(_ identifier: String){
self.segueIdentifier = identifier
self.performSegue(withIdentifier: self.segueIdentifier, sender: nil)
}
override open func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == segueIdentifier{
//Remove Container View
if viewController != nil{
viewController.view.removeFromSuperview()
viewController = nil
}
//Add to dictionary if isn't already there
if ((self.containerViewObjects[self.segueIdentifier] == nil)){
viewController = segue.destination
self.containerViewObjects[self.segueIdentifier] = viewController
}else{
for (key, value) in self.containerViewObjects{
if key == self.segueIdentifier{
viewController = value
}
}
}
self.addChildViewController(viewController)
viewController.view.frame = CGRect(x: 0,y: 0, width: self.view.frame.width,height: self.view.frame.height)
self.view.addSubview(viewController.view)
viewController.didMove(toParentViewController: self)
}
}