自定义视图标签不会改变

Custom View Label Won't Change

我的视图包含一个按钮,该按钮带来一个自定义视图,其中包含一个 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
    }
}

我已经为源代码创建了一个link,这样会更容易理解source code

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

你的 AlertTextViewController.

中的 var lblText: String? 怎么样

也许它被你的声明覆盖了(出于某种原因我无法解释)?

或者等等,我被

弄糊涂了

var alertText = AlertTextViewController()

正如您所展示的那样,创建和展示 ViewController alertalertTextalert 有什么关系?

编辑:

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

在您的警报中执行此操作ViewController,它会起作用。我不知道你的最终目的是什么,这可能不是最优雅的解决方案。我已经创建了一个插座,containerView 到您的 class.