我有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)
}
}
}
有人对我该如何处理有任何想法吗?现在,因为我在按钮功能中添加了tagVal
,所以yearOne
又名mainvc
似乎没有成员。
您需要在SearchPage视图控制器上设置一个值来捕获标记。
class SearchPage: UIViewController {
var tag: Int?
...
}
然后在您的switch语句中,您应该执行以下操作。
case 0:
print("ONEEE")
let destination = SearchPage()
destination.tag = sender.tag // <- update the tag value here
navigationController?.pushViewController(destination, animated: true)
您还应该删除let mainVC = yearOne()
,因为这是yearOne
类的新实例,并且与您以前创建的yearOne
没有关系。
您可以在SearchPage
中保留对原始YearOne
的引用:
class SearchPage: UIViewController {
var vc: YearOne?
}
然后您的切换案例代码将如下所示:
print("ONEEE")
let destination = SearchPage()
destination.vc = self
navigationController?.pushViewController(destination, animated: true)