0
votes

I have two View Controllers. The first UIViewController 'DiscrimUIViewCollection' has 5 buttons, each linked to one @IBAction func showAdvicePopUp(_ sender: UIButton) in the swift file. I can use the print(sender.currentTitle) successfully and it prints out the title to each of the 5 buttons.

In the Second View Controller, 'PopUpViewContoller' I have a UILabel that I would like to set the Text of from the Title of the sender button from the first controller.

I have used various func prepareForSegue(segue: UIStoryboardSegue, sender: UIButton?) commands, but the problem is that the 'PopUpViewController' just does not receive the 'sender.currentTitle' from the 'DiscrimUIViewCollection'.

I've been at this 2 days now and cannot find the answer.

I want the Value of var updateTheLabel: String? to receive the sender.currentTitle from the 'DiscrimUIViewCollection' and I think that it will all work.

I don't have any Segue arrows on the main.storyboard as I've done it all programmatically.

I've included the code below for you to review.

Thanks in advance.

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
          //}
       }
}

And here is the PopUpViewController file:

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!


}
2
Where is the code where you created segue programatically?iOSArchitect.com

2 Answers

1
votes

You can present the popOverVC and pass the data. There is no need to assign it a frame and add it as a subview if you want to present it in fullscreen.

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

Here is the complete working code:

The PopOverVC animates in and out with other code and sets the UILabel within.

@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!

}