将 sender.currentTitle UIButton 文本发送到 PopUpViewController 中的 UILabel 而不是发送到 PopUpViewController
Sending sender.currentTitle UIButton text to UILabel in PopUpViewController not sending to through to PopUpViewController
我有两个视图控制器。
第一个 UIViewController 'DiscrimUIViewCollection' 有 5 个按钮,每个链接到 swift 文件中的一个 @IBAction func showAdvicePopUp(_ sender: UIButton)。
我可以成功使用 print(sender.currentTitle) 并将标题打印到 5 个按钮中的每一个。
在第二个视图控制器中,'PopUpViewContoller'我有一个 UILabel,我想从第一个控制器的发件人按钮的标题中设置文本。
我使用了各种 func prepareForSegue(segue: UIStoryboardSegue, sender: UIButton?) 命令,但问题是 'PopUpViewController' 只是没有从 [=31= 接收到 'sender.currentTitle' ].
我已经在这 2 天了,找不到答案。
我想要 var updateTheLabel: String 的值?从 '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!
}
您可以呈现popOverVC并传递数据。如果您想全屏显示它,则无需为其分配框架并将其添加为子视图。
@IBAction func showAdvicePopUp(_ sender: UIButton) {
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "advicePopUpID") as! PopUpViewController
buttonText = sender.currentTitle!
popOverVC.updateTheLabel = buttonText
popOverVC.modalPresentationStyle = .fullScreen // to present on full screen
self.present(popOverVC, animation : true, completion : nil)
}
这是完整的工作代码:
PopOverVC 使用其他代码动画进出并在其中设置 UILabel。
@IBAction func showAdvicePopUp(_ sender: UIButton) {
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "advicePopUpID") as! PopUpViewController
buttonText = sender.currentTitle!
popOverVC.updateTheLabel = buttonText
self.addChild(popOverVC)
popOverVC.view.frame = self.view.frame
self.view.addSubview(popOverVC.view)
popOverVC.didMove(toParent: (self))
buttonText = sender.currentTitle!
}
我有两个视图控制器。 第一个 UIViewController 'DiscrimUIViewCollection' 有 5 个按钮,每个链接到 swift 文件中的一个 @IBAction func showAdvicePopUp(_ sender: UIButton)。 我可以成功使用 print(sender.currentTitle) 并将标题打印到 5 个按钮中的每一个。
在第二个视图控制器中,'PopUpViewContoller'我有一个 UILabel,我想从第一个控制器的发件人按钮的标题中设置文本。
我使用了各种 func prepareForSegue(segue: UIStoryboardSegue, sender: UIButton?) 命令,但问题是 'PopUpViewController' 只是没有从 [=31= 接收到 'sender.currentTitle' ].
我已经在这 2 天了,找不到答案。
我想要 var updateTheLabel: String 的值?从 '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!
}
您可以呈现popOverVC并传递数据。如果您想全屏显示它,则无需为其分配框架并将其添加为子视图。
@IBAction func showAdvicePopUp(_ sender: UIButton) {
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "advicePopUpID") as! PopUpViewController
buttonText = sender.currentTitle!
popOverVC.updateTheLabel = buttonText
popOverVC.modalPresentationStyle = .fullScreen // to present on full screen
self.present(popOverVC, animation : true, completion : nil)
}
这是完整的工作代码:
PopOverVC 使用其他代码动画进出并在其中设置 UILabel。
@IBAction func showAdvicePopUp(_ sender: UIButton) {
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "advicePopUpID") as! PopUpViewController
buttonText = sender.currentTitle!
popOverVC.updateTheLabel = buttonText
self.addChild(popOverVC)
popOverVC.view.frame = self.view.frame
self.view.addSubview(popOverVC.view)
popOverVC.didMove(toParent: (self))
buttonText = sender.currentTitle!
}