【发布时间】:2019-06-25 01:35:41
【问题描述】:
我正在尝试将数据从 UIViewController 发送到 UIView。我到处看了看,但似乎没有一个能正常工作。如下所示,当用户选择特定行时,我试图将 popup.priceLabel.text 和 popup.notificationLabel.text 从 UIViewcontroller 发送到 UIView。我尝试使用 segue、协议和其他方法,但似乎没有任何效果。以下是我想要完成的(我知道它不正确,但只是为了说明意图):
//UIViewController
class CouponsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("selected = \(indexPath.row)")
let popup = PopUpWindow()
popup.priceLabel.text = "100.00"
popup.notificationLabel.text = "Store"
handleShowPopUp()
}
@objc func handleShowPopUp() {
popUpWindow.showSuccessMessage = success
success = !success
popUpWindow.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
popUpWindow.alpha = 0
}
}
//UIView
class PopUpWindow: UIView {
var delegate: PopUpDelegate?
var showSuccessMessage: Bool? {
didSet {
guard showSuccessMessage != nil else { return }
priceLabel.text = ""
notificationLabel.text = ""
priceLabel.textColor = UIColor(red: 147/255, green: 227/255, blue: 105/255, alpha: 1)
}
}
}
【问题讨论】:
-
在您的示例中,您在显示视图之前设置了 showSuccessMessage。发生这种情况时,您的 didSet 属性观察者会在放大视图之前将标签文本设置为空字符串。
-
谢谢-那么我需要延迟或将代码放在其他位置吗?显示的内容有效但未及时设置是您所说的吗?
-
你需要先设置showSuccessMessage,然后再设置你的两个标签的文字。
-
对不起,我不确定我是否误解了这个建议:您的意思是将 showSuccessMessage 中的值设置为某个值而不是“”?如果是这样,我试过了,它仍然没有更新我在表格视图函数中设置的值
-
如果你能举个例子,我将不胜感激我尝试了不同的方法,但没有任何效果
标签: swift uitableview uiview segue popupwindow