gpt4 book ai didi

ios - 将 TitleLabel 从 UIButton 发送到不同 UIView Controller 中的 UILabel,但未发送

转载 作者:行者123 更新时间:2023-11-28 23:23:21 24 4
gpt4 key购买 nike

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

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

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

感谢您的审阅。

这是我在第一个 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 的代码:

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 文件。

我将以下代码放在 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/

谢谢你们的帮助,每条小评论都让我思考。
这是一个很大的学习曲线!

关于ios - 将 TitleLabel 从 UIButton 发送到不同 UIView Controller 中的 UILabel,但未发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59310762/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com