将TitleLabel从UIButton发送到另一个UIView Controller中的UILabel,但未发送

问题描述 投票:-1回答:1

我有一个UIViewController,我试图将UIButton的titleLabel发送到另一个UIView Controller中保存的UILabel。

我遵循了与以前工作正常的方法相同的步骤和模式,但是标题文本只是没有传递到下一个VC。

我有一个名为MtsCardsButton的Button类,但这只是设置按钮的动画和外观。

谢谢您的审查。

这是我在第一个VC中的Button代码:

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!


}

这是第二个接收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
   }
  }
ios swift xcode uibutton uilabel
1个回答
0
投票

因此,在研究了各种网站很多小时后,我找到了答案。我需要添加代码并重新放置代码。我更改了情节提要ID以匹配DiscrimUIViewCollection.swift文件。

我将以下代码放在MTSCardsPage.swift文件中'switch'语句的底部。

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

}

而且令我高兴的是,一切正常!我用来帮助我最多的网站就是这个网站:https://fluffy.es/3-ways-to-pass-data-between-view-controllers/

感谢您的协助,每个小小的评论都让我思考。学习曲线很大!

© www.soinside.com 2019 - 2024. All rights reserved.