我正在尝试使用TableViewController和ViewController之间的segue传递包含“Bar Name”值及其各自“Street Name”的两个字符串值。我使用的是名为HomeController
的类,另一个名为BarPage
。我为主屏幕准备了以下代码,这是一个表视图控制器保持单元格,单击时,应该执行我已经包含在故事板上的segue。我删除了一些解释这个问题的冗余代码:
import UIKit
import Firebase
class HomeController: UIViewController, UITableViewDelegate, UITableViewDataSource{
let barsArray = [Data(image: "bar1", street: "123 North Street"),
Data(image: "bar2", street: "345 South Street"),
Data(image: "bar3", street: "678 West Street")]
let tableView: UITableView = {
let tv = UITableView()
tv.separatorStyle = .none
tv.allowsSelection = true
return tv
}()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return barsArray.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! BarCell
cell.barImageView.image = UIImage(named: barsArray[indexPath.item].image!)
cell.barCoverLabel.text = "Street: " + barsArray[indexPath.item].street!
return cell
}
var locationName : String?
var locationStreet : String?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let barPage = segue.destination as! BarPage
barPage.barName = locationName!
barPage.barStreet = locationStreet!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
locationName = self.barsArray[indexPath.row].image
locationStreet = self.barsArray[indexPath.row].street
// WHEN USER PRESSES A ROW, PERFORM SEGUE
performSegue(withIdentifier: "mySegue", sender: self)
// Then enter the second view controller
present(BarPage(), animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.backgroundColor = .black
setUpTableView()
}
func setUpTableView(){
tableView.dataSource = self
tableView.delegate = self
tableView.register(BarCell.self, forCellReuseIdentifier: "cell")
view.addSubview(tableView)
.... add anchors and constraints for table view ..........
}
}
var home: HomeController?
class BarCell: UITableViewCell{
... add custom styling for each cell of the table view ....
override init(style: UITableViewCellStyle, reuseIdentifier: String?){
super.init(style: style, reuseIdentifier: reuseIdentifier)
setup()
}
func setup(){
.......... add anchors and constraints for the cell..........
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
以下是View Controller BarPage
中应该从他的segue中检索字符串的代码。
import UIKit
import Firebase
class BarPage: UIViewController{
let homeController = HomeController()
// These, in theory, should be the variables that retrieve the strings through my segue
var barName = String()
var barStreet = String()
public let barStreet: UILabel = {
var label = UILabel()
label.text = "The street is: "// + barStreet
label.textColor = .black
label.font = UIFont.boldSystemFont(ofSize: 16)
return label
}()
let barImage: UIImageView = {
let iv = UIImageView()
iv.image = UIImage(named: barName) // Should retrieve the image based on the string that is passed
iv.contentMode = .scaleAspectFill
iv.backgroundColor = .yellow
//iv.clipsToBounds = true
return iv
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.mint()
view.addSubview(barStreet)
view.addSubview(barImage)
positionElements()
}
func positionElements(){
.......... add anchors and constraints for the elements..........
}
}
如果我在不包含segue的情况下运行我的代码,则转换工作正常。但是,如果我包含Segue,我会收到以下错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'Receiver (<ProjectName.HomeController: 0x7f9fcec0fb10>) has
no segue with identifier 'mySegue''
如果事实上我的故事板中有一个带有该标识符的segue,那怎么可能呢?这是我的故事板的图片,显示了我正在使用的标识符:
正如您在右侧所看到的,相同的标识符与我的故事板中的segue匹配,但我的错误表明无法找到它。我该如何解决这个问题?这是因为我试图从TableViewController执行Segue吗?对于任何模棱两可的道歉,第一个月成为Swift用户。任何能指出正确方向的提示/建议都会非常有帮助。
答案 0 :(得分:0)
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "mySegue", sender: indexPath.row)
}
答案 1 :(得分:0)
在准备segue时添加这样的内容:
if let selectedIndex = tableView.indexPathForSelectedRow {
barPage.barStreet = barsArray[selectedIndex.row].street
...
}
并确保您正在调用self.performSegue ...