Updating Content In Container View

ε祈祈猫儿з 提交于 2021-01-01 06:44:21

问题


I have a Container View inside a View Controller. When the user presses a button the Container View is to be displayed, but the text on the Container View is based on which button the user pressed. To do this I need to update the Container View but I am not sure on how to do this.

This is my Container View Code:

override func viewDidLoad() {
        super.viewDidLoad()

        Cancel_Button.hidden = true
        Save_Button.hidden = true

        let Storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let ViewController: View = Storyboard.instantiateViewControllerWithIdentifier("View") as! View
        Title_String = ViewController.String
        Title_Label.text = Title_String
    }

And this is the code in my View Controller:

@IBOutlet var View_Controller_Popup: UIView!
var String = String()

override func viewDidLoad() {
        super.viewDidLoad()


        View_Controller_Popup.hidden = true
    }

@IBAction func Button_Pressed(sender: AnyObject) {
        let Storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let ViewController: ContainerView = Storyboard.instantiateViewControllerWithIdentifier("Container") as! ContainerView
        self.String = "Specific Title"
        ViewController.Title_String = self.String
        self.View_Controller_Popup.hidden = false
    }

So how do I reload the Container View so that when a button is pressed, the Container View is displayed with label text based on the button pressed.

Update:

As some people cannot understand the issue I am having I will reword the problem with images.

This is my View:

Images have been deleted

As you can see there are many Buttons (with icons). When the user presses the button I want a container view to pop up showing information:

So for each button the Label text has to be different. But currently in the app the string is blank and so the Label text is empty:

I believe that this is because I am not updating the code in the container view, and so the string is still empty.

So the issue is that the container view is not displaying the title label correctly, and I think this is due to not updating the container view, but I don't actually know how to update the container view.


回答1:


try using delegate protocol your containerView should extend parent Delegate protocol, and simply implement function with arguments func updateContent(id: Int)

 protocol ContentDelegate {
    func updateContent(id: Int)
 }

 class ParentViewController: UIViewController {
    var delegate: ContentDelegate?

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if (segue.identifier == "containerController") {
            let containerVC = segue.destination  as!   ChildContainerViewController

            self.delegate = containerVC
        }        
    }
}

class ChildContainerViewController: UIViewController, ContentDelegate {
   func updateContent(id: Int) {
     // your code
   }
}



回答2:


I have found solutions for a similar type problem where I am using container view to display specific data related user selection in table view.

  1. MainViewController as Base view which contains container view which is PageViewController.
  2. MainViewController have 3 segmentedControl with 3 segments.
  3. When a user selects new segment I need to update data inside container view to show segment related data.

Basically updating container view from it base viewController.

After searching a lot found solution :

  1. Basically, container view is childViewControllers inside base ViewControllers.

  2. We just need to find right childViewControllers to update data.

If you have only on contianer view :

let vc  = self.childViewControllers[0] as! PageViewController
vc.segment = selectedSegment
vc.viewWillAppear(true)

Or if you have multiple childViewControllers :

for controller in self.childViewControllers {
    if controller.isKind(of: PageViewController.self) {
        let vc  = controller as! PageViewController
        vc.segment = selectedSegment
        vc.viewWillAppear(true)
    }
}



回答3:


This is how I did it using Swift 5. You basically iterate through the childs to get the reference of the VC you want. This works regardless if there is only one or multiple child view controllers.

 if let vc = children.filter({$0 is ContainerViewController}).first as? ContainerViewController {
        vc.setupView()
    }



回答4:


I am not entirely sure what you are asking but I will attempt to answer. Let me know if I'm misunderstanding.

The key here is really just your architecture. I would propose having unique action methods for each button. This would look something like:

@IBAction func ButtonOnePressed(sender: AnyObject) {
    initializeContainerView(withTitle: "Button One Title")
}

@IBAction func ButtonTwoPressed(sender: AnyObject) {
    initializeContainerView(withTitle: "Button Two Title")
}

func initializeContainerView(withTitle title: String) {
    let Storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let ViewController: ContainerView = Storyboard.instantiateViewControllerWithIdentifier("Container") as! ContainerView
    ViewController.Title_String = title
    self.View_Controller_Popup.hidden = false
}

However, if you prefer/require using the same action method between the buttons, add tags to your buttons UIButton.tag. Then just use a switch statement in your action method to check the sender.tag and run code for that case. This would look something like:

@IBAction func Button_Pressed(sender: AnyObject) {
    switch sender.tag {
    case buttonOneTagValue:
        initializeContainerView(withTitle: "Button One Title")
    case buttonTwoTagValue:
        initializeContainerView(withTitle: "Button Two Title")
    default:
        break
}

func initializeContainerView(withTitle title: String) {
    let Storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let ViewController: ContainerView = Storyboard.instantiateViewControllerWithIdentifier("Container") as! ContainerView
    ViewController.Title_String = title
    self.View_Controller_Popup.hidden = false
}



回答5:


In your initializeContainerView method, change:

self.Day_Period_String = title
ViewController.Title_String = self.Day_Period_String

to:

ViewController.Title_String = title

And in your Container View Controller, remove:

Title_String = ViewController.Day_Period_String

Lastly change this line:

var Title_String = String()

to:

var Title_String: String!

If this doesn't work, I'm going to have you add a few print lines. Unfortunately I've updated to XCode 8 (and thus Swift 3) so cannot run your code.



来源:https://stackoverflow.com/questions/39938038/updating-content-in-container-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!