gpt4 book ai didi

ios - 将 sender.currentTitle UIButton 文本发送到 PopUpViewController 中的 UILabel 而不是发送到 PopUpViewController

转载 作者:行者123 更新时间:2023-11-28 23:23:29 26 4
gpt4 key购买 nike

我有两个 View Controller 。第一个 UIViewController 'DiscrimUIViewCollection' 有 5 个按钮,每个链接到 swift 文件中的一个 @IBAction func showAdvicePopUp(_ sender: UIButton)。我可以成功使用 print(sender.currentTitle) 并将标题打印到 5 个按钮中的每一个。

在第二个 View Controller “PopUpViewContoller”中,我有一个 UILabel,我想从第一个 Controller 的发件人按钮的标题中设置文本。

我已经在这 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 并传递数据。如果您想以全屏方式呈现它,则无需为其分配框架并将其添加为 subview 。

@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)
}

关于ios - 将 sender.currentTitle UIButton 文本发送到 PopUpViewController 中的 UILabel 而不是发送到 PopUpViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59261455/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com