如何解决尝试使用 unwindSegue 并将信息发送回我的集合视图
我被困在这个问题上已经有一段时间了,我无处可求。基本上,我想使用 unwindSegue
(除非我可以通过不同的路线实现我的目标)将我的信息从我的表视图(A)发送到我的集合视图(B),但我不知道我是什么我失踪了。
我的代码如下:
表格视图(A):
import UIKit
class EditBeerTableViewController: UITableViewController {
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var companyTextField: UITextField!
@IBOutlet weak var countTextField: UITextField!
@IBOutlet weak var purchasePriceTextField: UITextField!
@IBOutlet weak var saveButton: UIBarButtonItem!
var beer: Beer?
override func viewDidLoad() {
super.viewDidLoad()
if let beer = beer {
nameTextField.text = beer.name
companyTextField.text = beer.company
countTextField.text = beer.count
purchasePriceTextField.text = beer.purchasePrice
}
updateSaveButton()
}
func updateSaveButton() {
let nameText = nameTextField.text ?? ""
let companyText = companyTextField.text ?? ""
let countText = countTextField.text ?? ""
let purchasePriceText = purchasePriceTextField.text ?? ""
saveButton.isEnabled = !nameText.isEmpty && !companyText.isEmpty && !countText.isEmpty && !purchasePriceText.isEmpty
}
@IBAction func textEditingChanged(_ sender: UITextField) {
updateSaveButton()
}
@IBAction func unwindToBeerCollectionView(for unwindSegue: UIStoryboardSegue) {
print("Save Button pressed")
}
@IBAction func saveButtonpressed(_ sender: Any) {
print("Save Button pressed")
}
override func prepare(for segue: UIStoryboardSegue,sender:
Any?) {
super.prepare(for: segue,sender: sender)
guard segue.identifier == "saveUnwind" else { return }
let name = nameTextField.text ?? ""
let company = companyTextField.text ?? ""
let count = countTextField.text ?? ""
let purchasePrice = purchasePriceTextField.text ?? ""
beer = Beer(name: name,company: company,count: count,purchasePrice: purchasePrice)
}
查看控制器(B):
import UIKit
class BeerViewController: UIViewController {
@IBOutlet weak var beerCollectionView: UICollectionView!
@IBOutlet weak var beerHeightConstraint: NSLayoutConstraint!
var beers: [Beer] = [
Beer(name: "Budweiser",company: "Budweiser",count: "10",purchasePrice: "1.50"),Beer(name: "Budlight",company: "Budlight",count: "16",purchasePrice: "1.00"),Beer(name: "Tangerine Wheat",company: "Lost Coast",count: "4",purchasePrice: "2.25"),Beer(name: "Coors Light",company: "Coors",count: "150",Beer(name: "Kokanee",company: "Kokanee",count: "13",purchasePrice: "1.00")
]
override func viewDidLoad() {
super.viewDidLoad()
beerCollectionView.dataSource = self
beerCollectionView.delegate = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
beerCollectionView.dataSource = self
beerCollectionView.delegate = self
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
}
@IBAction func unwindToBeerCollectionView(for unwindSegue: UIStoryboardSegue) {
}
}
extension BeerViewController: UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
if collectionView == beerCollectionView {
self.performSegue(withIdentifier: "BeerEdit",sender: nil)
} else {
}
if let cell = collectionView.cellForItem(at: indexPath) {
cell.contentView.backgroundColor = #colorLiteral(red: 1,green: 0.4932718873,blue: 0.4739984274,alpha: 1)
}
}
func collectionView(_ collectionView: UICollectionView,diddeselectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) {
cell.contentView.backgroundColor = nil
}
}
func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
return beers.count
}
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BeerCardCell",for: indexPath) as! BeerCollectionViewCell
let beer = beers[indexPath.row]
cell.update(with: beer)
print(beer.name)
return cell
}
func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeforItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.bounds.width
let height = width / 2.25
return CGSize(width: width,height: height)
}
override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
if segue.identifier == "BeerEdit" {
let indexPath = beerCollectionView.indexPathsForSelectedItems?.first
let beer = beers[indexPath!.row]
let navController = segue.destination as!
UINavigationController
let editBeerTableViewController =
navController.topViewController as!
EditBeerTableViewController
editBeerTableViewController.beer = beer
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。