将 TitleLabel 从 UIButton 发送到不同 UIView 控制器中的 UILabel,但未发送 [英] Send TitleLabel from UIButton to a UILabel in a different UIView Controller, But it's not being sent

查看:17
本文介绍了将 TitleLabel 从 UIButton 发送到不同 UIView 控制器中的 UILabel,但未发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIViewController,我试图将 UIButton 的 titleLabel 发送到不同 UIView 控制器中的 UILabel.

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

我遵循了与以前的方法相同的步骤和模式,效果很好,但标题文本没有传递到下一个 VC.

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.

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

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

感谢您的审核.

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

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!


}

这是第二个接收VC的代码:

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

推荐答案

因此,经过数小时研究各种网站后,我找到了答案.我需要添加代码并重新定位代码.我更改了 Storyboard ID 以匹配 DiscrimUIViewCollection.swift 文件.

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.

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

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)

}

令我高兴的是,一切正常!我曾经对我帮助最大的网站是这个:https://fluffy.es/3-ways-to-pass-data-between-view-controllers/

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!

这篇关于将 TitleLabel 从 UIButton 发送到不同 UIView 控制器中的 UILabel,但未发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆