【发布时间】:2020-04-03 07:10:28
【问题描述】:
我有两个视图控制器。 第一个 UIViewController 'DiscrimUIViewCollection' 有 5 个按钮,每个按钮都链接到 swift 文件中的一个 @IBAction func showAdvicePopUp(_ sender: UIButton)。 我可以成功使用 print(sender.currentTitle) 并打印出 5 个按钮中的每一个的标题。
在第二个视图控制器“PopUpViewContoller”中,我有一个 UILabel,我想从第一个控制器的发送者按钮的标题中设置文本。
我使用了各种 func prepareForSegue(segue: UIStoryboardSegue, sender: UIButton?) 命令,但问题是“PopUpViewController”没有从“DiscrimUIViewCollection”接收到“sender.currentTitle”。
我已经在这两天了,找不到答案。
我想要 var updateTheLabel 的值:字符串?从“DiscrimUIViewCollection”接收 sender.currentTitle,我认为这一切都会奏效。
我在 main.storyboard 上没有任何 Segue 箭头,因为我都是以编程方式完成的。
我已包含以下代码供您查看。
提前致谢。
class DiscrimUIViewCollection: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
public var buttonText = ""
@IBAction func showAdvicePopUp(_ sender: UIButton) {
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "advicePopUpID") as! PopUpViewController
self.addChild(popOverVC)
popOverVC.view.frame = self.view.frame
self.view.addSubview(popOverVC.view)
popOverVC.didMove(toParent: (self))
buttonText = sender.currentTitle!
print(sender.currentTitle!)
print(buttonText) //THIS WORKS!
}
func prepareForSegue(segue: UIStoryboardSegue, sender: UIButton?) {
//if segue.identifier == "advicePopUpID" {
let controller = segue.destination as! PopUpViewController
controller.updateTheLabel = buttonText
//}
}
}
这里是 PopUpViewController 文件:
class PopUpViewController: UIViewController {
// This file is to set the setting of the UIView Controller and how it appears.
//var desiredLabelValue: String?
var updateTheLabel: String?
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.black.withAlphaComponent(0.6)
self.showAnimate()
adviceTitle.text = updateTheLabel
//print(updateTheLabel!)
//adviceTitle?.text = desiredLabelValue
//To set the text of Header
//adviceTitle?.text = "Yellow Advice"
// Do any additional setup after loading the view.
}
@IBAction func closePopUp(_ sender: Any) {
//self.view.removeFromSuperview()
//self.removeAnimate()
print(updateTheLabel)
}
func showAnimate() {
self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
self.view.alpha = 0.0
//popUpArea.backgroundColor = colourOrange
UIView.animate(withDuration: 0.25, animations: {
self.view.alpha = 1.0
self.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
//print(self.adviceText!)
//print(self.adviceTitle!)
//self.adviceTitle?.text = "Yellow Advice"
//self.adviceTitle?.text = self.desiredLabelValue
//print(self.adviceTitle?.text!)
//Set the Background Colour of the popup box
//depending on what the Title states.
if self.adviceTitle.text == "Red Advice" {
self.popUpArea.backgroundColor = mtsRed
} else if self.adviceTitle?.text == "Orange Advice" {
self.popUpArea.backgroundColor = mtsOrange
} else if self.adviceTitle.text == "Yellow Advice" {
self.popUpArea.backgroundColor = mtsYellow
} else if self.adviceTitle.text == "Green Advice" {
self.popUpArea.backgroundColor = mtsGreen
} else if self.adviceTitle.text == "Blue Advice" {
self.popUpArea.backgroundColor = mtsBlue
}
})
}
func removeAnimate() {
UIView.animate(withDuration: 0.25, animations: {
self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
self.view.alpha = 0.0;
}) { (success:Bool) in
self.view.removeFromSuperview()
}
}
@IBOutlet weak var adviceTitle: UILabel!
@IBOutlet weak var adviceText: UILabel!
@IBOutlet weak var popUpArea: UIView!
}
【问题讨论】:
-
您以编程方式创建 segue 的代码在哪里?
标签: ios swift uilabel segue popover