【问题标题】:Updating Content In Container View在容器视图中更新内容
【发布时间】:2017-02-17 16:20:02
【问题描述】:

我在视图控制器中有一个容器视图。当用户按下按钮时,Container View 将被显示,但 Container View 上的文本取决于用户按下的按钮。为此,我需要更新容器视图,但我不确定如何执行此操作。

这是我的容器查看代码:

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
    }

这是我的视图控制器中的代码:

@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
    }

那么我该如何重新加载容器视图,以便在按下按钮时,容器视图会根据按下的按钮显示标签文本。

更新:

由于有些人无法理解我遇到的问题,我将用图像重新表述问题。

这是我的观点:

图片已被删除

如您所见,有许多按钮(带有图标)。当用户按下按钮时,我希望容器视图弹出显示信息:

因此,对于每个按钮,标签文本必须不同。但目前在应用程序中,字符串为空白,因此标签文本为空:

我相信这是因为我没有更新容器视图中的代码,所以字符串仍然是空的。

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

【问题讨论】:

  • 如果您使用的是故事板,那么您可以使用嵌入 segue 设置包含的视图控制器。在您包含的 ViewController 中,您可以在 prepareForSegue 中获取对包含的视图控制器的引用并寻找嵌入 segue。将引用存储在属性中后,您可以随时在其上调用函数。
  • 我正在使用故事板,你能告诉我如何在代码中做到这一点吗?

标签: ios swift uicontainerview


【解决方案1】:

我不完全确定您在问什么,但我会尝试回答。如果我误解了,请告诉我。

这里的关键实际上只是您的架构。我建议为每个按钮设置独特的操作方法。这看起来像:

@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
}

但是,如果您更喜欢/要求在按钮之间使用相同的操作方法,请将标签添加到您的按钮 UIButton.tag。然后只需在您的操作方法中使用 switch 语句来检查 sender.tag 并针对这种情况运行代码。这看起来像:

@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
}

【讨论】:

  • 我试过你的代码和其他代码,但我感觉你好像误解了这个问题。请参阅我更新的问题。
  • 您可以尝试使用 ViewController 更新容器视图 .view.setNeedsDisplay() 您可以将此行放在initializeContainerView 作为最后一行。
  • 快点,你能硬编码一个 UILabel.text 值来看看它是否为非空吗?
  • 它似乎工作正常。我在容器视图中添加了打印,它在 SetNeedsDisplay() 之前和之后打印一个空字符串。
  • 我可以把文件发给你看看你能不能找到问题,但我真的很困惑为什么它不起作用。
【解决方案2】:

在您的 initializeContainerView 方法中,更改:

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

到:

ViewController.Title_String = title

然后在您的容器视图控制器中,删除:

Title_String = ViewController.Day_Period_String

最后改变这一行:

var Title_String = String()

到:

var Title_String: String!

如果这不起作用,我会让你添加一些打印行。不幸的是,我已经更新到 XCode 8(以及 Swift 3),所以无法运行您的代码。

【讨论】:

  • 好的,所以我在代码周围添加了一些打印,看起来标签文本设置正确但未显示。所以标题字符串不为空,标签文本也不为空,但是当将这个添加到文本中时:.text = Title_String +“Testing”,它只显示“Testing”。那么标签是否没有正确更新?
  • 嗯。这就是为什么我建议使用 setNeedsDisplay 的原因,因为您的数据似乎设置正确,因为它应该强制 UI 更新。也许您的 UILabel 宽度太小,因此不显示文本?确认这一点的一种方法是打印 UILabel 宽度,或者您可以在初始化和更新文本后添加一行以确保大小适合文本:Title_Label.sizeToFit()
  • 但它显示的是文本,而不是我想要的文本,但是当将标签打印到控制台时,它会打印我想要打印的内容。
  • 容器视图 UILabel 显示什么文本?您是否在情节提要中添加了一些默认文本或其他内容?
  • 如果我这样做: self.Title_Label.text = "Text Here" 它会显示 Text Here。如果我这样做: self.Title_Label.text = self.Title_String + "Text Here" 它只显示 Text Here。
【解决方案3】:

尝试使用委托协议 您的 containerView 应该扩展父 Delegate 协议,并且只需使用参数 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
   }
}

【讨论】:

  • 如果我不使用情节提要怎么办?详细说明如何在没有prepare(foor segue:)的情况下获取containerVC
【解决方案4】:

我找到了类似类型问题的解决方案,我使用容器视图在表格视图中显示与特定数据相关的用户选择。

  1. MainViewController 作为基础视图,其中包含容器视图,即 PageViewController。
  2. MainViewController 有 3 个 segmentedControl 和 3 个段。
  3. 当用户选择新段时,我需要更新容器视图内的数据以显示段相关数据。

基本上从它的基础 viewController 更新容器视图。

搜索了很多找到的解决方案:

  1. 基本上,容器视图是基本 ViewControllers 中的 childViewControllers。

  2. 我们只需要找到合适的childViewControllers来更新数据。

如果您只有 contianer 视图:

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

或者,如果您有多个 childViewController:

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

【讨论】:

    【解决方案5】:

    这就是我使用 Swift 5 的方式。您基本上遍历子项以获取所需 VC 的引用。无论是否只有一个或多个子视图控制器,这都有效。

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

    【讨论】:

    • 最佳和更简单的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    • 2013-04-26
    • 2021-03-09
    • 1970-01-01
    相关资源
    最近更新 更多