Search code examples
iosswiftxcodeuibuttonuilabel

Send TitleLabel from UIButton to a UILabel in a different UIView Controller, But it's not being sent


I have one UIViewController that I am trying to send the titleLabel of a UIButton to a UILabel held within a different UIView Controller.

I have followed the same steps and pattern as within a previous method that worked fine, but the Title Text is just not getting passed onto the next VC.

I have a Button class called MtsCardsButton, but this just sets the animation and appearance of the button.

Thank you for reviewing.

Here is my code for the Button in the first VC:

import UIKit

class MTSCardsPage: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    //This is to make mtsCardsSetArray available to this ViewController
    let otherVC = MTSDiscriminators()
    mtsCardsSetArray2 = otherVC.mtsCardsSetArray

    let otherVC2 = MTSDiscriminators()
    allMtsDescriminatorsArray2 = otherVC2.allMtsDescriminatorsArray


    //Set Card Titles from Array
    Card1ButtonOutlet.setTitle(mtsCardsSetArray2[0], for: .normal)
    Card2ButtonOutlet.setTitle(mtsCardsSetArray2[1], for: .normal)
    Card3ButtonOutlet.setTitle(mtsCardsSetArray2[2], for: .normal)
    Card4ButtonOutlet.setTitle(mtsCardsSetArray2[3], for: .normal)
    Card5ButtonOutlet.setTitle(mtsCardsSetArray2[4], for: .normal)

    //Do any additional setup after loading the view.

}


var mtsCardsButton = MtsCardsButton()


func addActionToMtsCardsButton() {
     mtsCardsButton.addTarget(self, action: #selector(CardButton), for: .touchUpInside)


 }

//This is to TELL the Button to do something AND to goto
//the MTS Discriminators UIView.

var cardButtonPressed = ""

@IBAction func CardButton(_ sender: MtsCardsButton) {
    let secondVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "DiscrimUIViewCollection") as! DiscrimUIViewCollection
   cardButtonPressed = sender.currentTitle!
    secondVC.updateTheLabel2 = cardButtonPressed


   ////I'VE TRIED THIS SECTION INSTEAD OF ABOVE AND IT STILL DOESN'T WORK.

//        func prepare(for segue: UIStoryboardSegue, sender: Any?)
//    {
//        if segue.destination is DiscrimUIViewCollection
//        {
//            let vc = segue.destination as? DiscrimUIViewCollection
//            vc?.updateTheLabel2 = cardButtonPressed
//        }
//    }

    //switch area to make button move when pressed
    switch cardButtonPressed {
    case mtsCardsSetArray2[0]:
        Card1ButtonOutlet.shakeMtsCardsButton()
    case mtsCardsSetArray2[1]:
        Card2ButtonOutlet.shakeMtsCardsButton()
    case mtsCardsSetArray2[2]:
        Card3ButtonOutlet.shakeMtsCardsButton()
    case mtsCardsSetArray2[3]:
        Card4ButtonOutlet.shakeMtsCardsButton()
    case mtsCardsSetArray2[4]:
        Card5ButtonOutlet.shakeMtsCardsButton()
    default:
        print("Error, unrecognised button pressed!")
    }  

    guard let destinationVC = storyboard?.instantiateViewController(withIdentifier: "DiscrimUIViewCollection") as? DiscrimUIViewCollection else {
        return
    }
    present(destinationVC, animated: true, completion: nil)

}


 //Outlet for sending anything to the MTS Card Button
 @IBOutlet weak var Card1ButtonOutlet: MtsCardsButton!
 @IBOutlet weak var Card2ButtonOutlet: MtsCardsButton!
 @IBOutlet weak var Card3ButtonOutlet: MtsCardsButton!
 @IBOutlet weak var Card4ButtonOutlet: MtsCardsButton!
 @IBOutlet weak var Card5ButtonOutlet: MtsCardsButton!


}

Here is the code for the second receiving VC:

class DiscrimUIViewCollection: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

@IBOutlet weak var discriminatorTitle: UILabel!

var updateTheLabel2: String?

@IBAction func discrimButtonPressed(_ sender: UIButton) {
    //action here to name what discriminator means.
    print(sender.currentTitle!)
}

@IBOutlet weak var collectionView: UICollectionView!

override func viewDidLoad() {
    super.viewDidLoad()

    Card1ButtonOutlet.setTitle(mtsCardsSetArray2[0], for: .normal)
    Card2ButtonOutlet.setTitle(mtsCardsSetArray2[1], for: .normal)
    Card3ButtonOutlet.setTitle(mtsCardsSetArray2[2], for: .normal)
    Card4ButtonOutlet.setTitle(mtsCardsSetArray2[3], for: .normal)
    Card5ButtonOutlet.setTitle(mtsCardsSetArray2[4], for: .normal)

    collectionView.dataSource = self
    collectionView.delegate = self
    self.collectionView.backgroundColor = .black

        discriminatorTitle.text = updateTheLabel2
        discriminatorTitle.font = UIFont(name: "Mukta Mahee", size: 18)
        discriminatorTitle.font = UIFont.boldSystemFont(ofSize: 18)
        discriminatorTitle.numberOfLines = 2
        discriminatorTitle.minimumScaleFactor = 0.1
        discriminatorTitle.baselineAdjustment = .alignCenters
        discriminatorTitle.textAlignment  = NSTextAlignment.center
        discriminatorTitle.clipsToBounds = true
        discriminatorTitle.backgroundColor = colourYellow
        discriminatorTitle.textColor = .black
        discriminatorTitle.layer.borderColor = UIColor.black.cgColor
        discriminatorTitle.layer.borderWidth = 2.0
        discriminatorTitle.layer.cornerRadius = 7
        discriminatorTitle.layer.shadowColor = UIColor.black.cgColor
        discriminatorTitle.layer.shadowOffset = CGSize(width: 0.0, height: 6.0)
        discriminatorTitle.layer.shadowRadius = 7
        discriminatorTitle.layer.shadowOpacity = 0.5
        discriminatorTitle.clipsToBounds      = false
        discriminatorTitle.layer.masksToBounds  = true


let layout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout
layout.sectionInset = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
layout.minimumInteritemSpacing = 2
layout.itemSize = CGSize(width: (self.collectionView.frame.size.width - 0)/1, height:self.collectionView.frame.size.height/3)
      //Do any additional setup after loading the view.

func numberOfSections(in collectionView: UICollectionView) -> Int {
       // 1
       return 1
   }
  }

Solution

  • So, after many hours of studying up various websites I found the answer. I needed to add code and re-position the code. I Changed the Storyboard ID to match the DiscrimUIViewCollection.swift file.

    I place the following code at the bottom of the 'switch' statement in the MTSCardsPage.swift file.

        //To capture the card title and store it for
           //preparation for changing based on Label.
    
        guard let DiscrimUIViewCollection : DiscrimUIViewCollection = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DiscrimUIViewCollection") as? DiscrimUIViewCollection else {
            return
        }
        DiscrimUIViewCollection.updateTheLabel = sender.currentTitle!
    
        present(DiscrimUIViewCollection, animated: true, completion: nil)
    
    }
    

    And to my delight, it all works fine! The website I used to help me the most was this one: https://fluffy.es/3-ways-to-pass-data-between-view-controllers/

    Thanks for your assistance guys, each little comment made me think.
    It's big learning curve!