【发布时间】:2020-04-06 05:01:34
【问题描述】:
我有一个 UIViewController,我试图将 UIButton 的 titleLabel 发送到另一个 UIView 控制器中保存的 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
}
}
【问题讨论】:
-
毫无意义。您创建
secondVC然后将其丢弃。您的代码中没有“下一个 vc”。 -
感谢您的意见,我对此很陌生,仍在学习。我看过一个可能的下一个 vc,但不确定如何添加它。我再次查看了代码,我无法制定任何建议“下一个 vc”的内容。就如一行代码那么简单吗?
-
很难说,因为我无法想象你认为你在做什么。之前这个“工作”的“模式”是什么?我敢打赌它实际上是非常不同的。通常的模式是 performSegue 和 prepareForSegue。如果您致电
instantiate,则没有转场,因此什么也不会发生。 -
@David_2877 你的 performSegue 或展示 Next VC 代码在哪里?向我们展示你将如何进入下一个 VC
-
我对代码进行了一些编辑,并提交了用于发送 swift 文件的整个代码。再次感谢您的宝贵时间。
标签: ios swift xcode uibutton uilabel