简体   繁体   English

更新容器视图中的内容

[英]Updating Content In Container View

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.所以问题是容器视图没有正确显示标题标签,我认为这是由于没有更新容器视图,但我实际上不知道如何更新容器视图。

try using delegate protocol your containerView should extend parent Delegate protocol, and simply implement function with arguments func updateContent(id: Int)尝试使用委托协议你的 containerView 应该扩展父委托协议,并简单地使用参数来实现函数 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
   }
}

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. MainViewController 作为基本视图,其中包含容器视图,即 PageViewController。
  2. MainViewController have 3 segmentedControl with 3 segments. MainViewController 有 3 个 segmentedControl 和 3 个部分。
  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.基本上从它的基础 viewController 更新容器视图。

After searching a lot found solution :经过大量搜索找到解决方案:

  1. Basically, container view is childViewControllers inside base ViewControllers.基本上,容器视图是基础 ViewControllers 中的 childViewControllers。

  2. We just need to find right childViewControllers to update data.我们只需要找到合适的 childViewControllers 来更新数据。

If you have only on contianer view :如果您只有 contianer 视图:

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

Or if you have multiple childViewControllers :或者,如果您有多个 childViewControllers :

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

This is how I did it using Swift 5. You basically iterate through the childs to get the reference of the VC you want.这就是我使用 Swift 5 的方式。您基本上是遍历孩子以获得您想要的 VC 的参考。 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()
    }

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 .但是,如果您更喜欢/需要在按钮之间使用相同的操作方法,请将标签添加到您的按钮UIButton.tag Then just use a switch statement in your action method to check the sender.tag and run code for that case.然后只需在您的操作方法中使用 switch 语句来检查 sender.tag 并运行该案例的代码。 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
}

In your initializeContainerView method, change:在您的initializeContainerView方法中,更改:

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.不幸的是,我已更新到 XCode 8(以及 Swift 3),因此无法运行您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM