【发布时间】:2023-03-17 12:13:01
【问题描述】:
我有 4 个不同的按钮指向同一个视图控制器 SearchPage(),
因此我在 yearOne 主类中分配了这些按钮,如下所示:
class yearOne: UICollectionViewController:
@objc func buttonAction(sender: UIButton!) {
var tagVal : NSInteger = sender.tag
switch sender.tag {
case 0:
print("ONEEE")
let destination = SearchPage()
navigationController?.pushViewController(destination, animated: true)
case 1:
print("TWOOO")
let destination = SearchPage()
navigationController?.pushViewController(destination, animated: true)
case 2:
print("THREEE")
let destination = SearchPage()
navigationController?.pushViewController(destination, animated: true)
case 3:
print("FOURRR")
let destination = SearchPage()
navigationController?.pushViewController(destination, animated: true)
default:
let destination = SearchPage()
navigationController?.pushViewController(destination, animated: true)
}
}
每个按钮都属于不同的类
class fallQuarterCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource:
let addButton : UIButton = {
let button = UIButton()//frame: CGRect(x: 345, y: 10, width: 30, height: 30))
button.setImage(UIImage(named: "addicon"), for: .normal)
button.imageEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
button.tag = 0
button.addTarget(self, action: #selector(yearOne.buttonAction), for: .touchUpInside)
return button
}()
class winterQuarterCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource:
let addButton : UIButton = {
let button = UIButton()//frame: CGRect(x: 345, y: 10, width: 30, height: 30))
button.setImage(UIImage(named: "addicon"), for: .normal)
button.imageEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
button.tag = 1
button.addTarget(self, action: #selector(yearOne.buttonAction), for: .touchUpInside)
return button
}()
第三个和第四个按钮以此类推。无论如何,该按钮确实有效,并且每个按钮都能正确打印并将我发送到SearchPage()
但是,在我的 SearchPage() 课程中,我试图回忆 sender.tag?这样我就可以使用 if/else 语句来确定按下/选择了哪个按钮。
let mainvc = yearOne()
let vc = fallQuarterCell()
let vc2 = winterQuarterCell()
let vc3 = springQuarterCell()
let vc4 = summerQuarterCell()
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
tableView.deselectRow(at: indexPath, animated: true)
if (mainvc.tagValue == 0){
if (resultSearchController.isActive){
vc.data.append(filteredCourses[indexPath.row])
UserDefaults.standard.set(vc.data, forKey: "SavedArray")
navigationController?.popViewController(animated: true)
}
else{
vc.data.append(allCourses[indexPath.row] as! String)
UserDefaults.standard.set(vc.data, forKey: "SavedArray")
navigationController?.popViewController(animated: true)
}
}
else if (mainvc.tagValue == 1){
if (resultSearchController.isActive){
vc2.data.append(filteredCourses[indexPath.row])
UserDefaults.standard.set(vc2.data, forKey: "SavedArray2")
vc2.tableView.reloadData()
navigationController?.popViewController(animated: true)
}
else{
vc2.data.append(allCourses[indexPath.row] as! String)
UserDefaults.standard.set(vc2.data, forKey: "SavedArray2")
vc2.tableView.reloadData()
navigationController?.popViewController(animated: true)
}
}
}
有没有人知道我该如何处理这个问题?现在因为我在按钮 func 中添加了 tagVal,所以 yearOne aka mainvc 似乎没有成员。
【问题讨论】:
标签: swift if-statement uibutton switch-statement