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

标签 ios swift uilabel segue popover

我有两个 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/

相关文章:

ios - 使用计时器的 arc4random() 后应用程序崩溃?

ios - 在第二次调用期间将数据填充到方法中的字典时出错

ios - performSegue - 它会停止进一步的代码执行吗?

ios - 计时器在 iOS 的 UITableView 单元格中无法正常工作

ios - 在UILabel中突出显示特定文本

ios - 将 CGPathRef 转换为 CGMutablePathRef

ios - 无法访问 tableview 属性以获取在自定义单元格中单击的按钮的索引

iphone - iOS 尝试将图像保存到相机胶卷但未成功

ios - 如何存储所有选定的按钮值并在 UILabel 上显示它们?

ios - 带有尾随空格的 UILabel 不能正确居中