【问题标题】:Custom View Label Won't Change自定义视图标签不会改变
【发布时间】:2015-07-20 13:34:29
【问题描述】:

我的视图包含一个按钮,它带来一个自定义视图,其中包含一个ContainerView 并且它有一个标签。当我将自定义视图放在前面时,标签文本为空。那么请问我的问题在哪里?

查看控制器代码:

@IBAction func PushAlert(sender: UIButton) {

    let alert = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("alertViewController") as! AlertViewController

    var alertText = AlertTextViewController()

    alertText.lblText = "DONE"

    alert.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
    alert.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
    self.presentViewController(alert, animated: true, completion: nil)

}

class AlertTextViewController: UIViewController {

    @IBOutlet weak var lblAlertText: UILabel!
    var lblText = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        lblAlertText.text = lblText
    }
}

我已经为源代码创建了一个链接,这将更容易理解source code

【问题讨论】:

  • 在你放在这里的源代码中,应该在哪里显示空白的标签?
  • 它应该显示在故事板@BrandonShega 中的“警报视图”文本下
  • 您设置标签文本的AlertTextViewController 与您的AlertViewController 中包含的不同,它是您正在创建的一个新实例,它会在方法结束。您需要向AlertViewController 添加一个可以向其传递文本的方法,并让该方法找到嵌入的AlertTextViewController 并在其上设置标签文本。

标签: ios swift uicontainerview


【解决方案1】:

AlertTextViewController 中的 var lblText: String? 怎么样。

也许它被您的声明所覆盖(由于某种原因我无法解释)?

或者等等,我很困惑

var alertText = AlertTextViewController()

当您展示创建和展示 ViewController alert 时。 alertTextalert 有什么关系?

编辑:

class AlertViewController: UIViewController {

@IBOutlet weak var container: UIView!

override func viewDidLoad() {
    super.viewDidLoad()
    var alertText: AlertTextViewController = self.childViewControllers[0] as! AlertTextViewController
    alertText.lblAlertText.text = "Test"
    // Do any additional setup after loading the view.
}

在您的 AlertViewController 中执行此操作,它可以工作。我不知道你的最终目的是什么,这可能不是最优雅的解决方案。我已经为你的班级创建了一个插座,即 containerView。

【讨论】:

  • 两者都在同一个视图中,所以在我呈现 AlertViewController 之前不应该调用 AlertTextViewController 吗?
  • 对不起,我混淆了一些变量名,我会看一下提供的源代码。
  • 我认为问题是在 AlertViewController 之前调用了 AlertTextViewController,因此它不会显示文本。不知道怎么解决?
  • 嗯,我对容器视图和嵌入式 segues 感到困惑,因为我没有使用它,但我真的无法理解您的警报和警报文本之间的联系。 alert 如何理解它将使用嵌入式 segue 来呈现您创建的 alertText 控制器?
  • 我希望我能知道这将解决我的问题:(
【解决方案2】:

我认为您可能有事件顺序问题。尝试在 viewWillAppear 中设置 lblAlertText.text 而不是 viewDidLoad。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 2011-04-22
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多