我想在用户选择其中一个分段控件选项时返回一个值。我收到了错误
void函数中出现意外的非void返回值。
如何让pageStatus
更改为用户点击的内容?
class ReportsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var reportList:[String] = []
var ref: DatabaseReference!
var handle: DatabaseHandle?
var pageStatus = "default"
@IBOutlet weak var reportsTableView: UITableView!
@IBAction func backButtonPressed(_ sender: UIButton) {
self.performSegue(withIdentifier: "reportsToHome", sender: self)
}
@IBOutlet weak var segmentControl: UISegmentedControl!
@IBAction func segmentControlPressed(_ sender: UISegmentedControl) {
switch segmentControl.selectedSegmentIndex
{
case 0:
pageStatus = "unclaimed";
case 1:
pageStatus = "complete";
case 2:
pageStatus = "cancelled";
default:
break;
}
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return reportList.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = reportList[indexPath.row]
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
handle = ref.child("Reports").observe(.childAdded, with: { (snapshot) in
if let reports = snapshot.value as? NSDictionary {
let firstName = reports["firstName"] as? String
let lastName = reports["lastName"] as? String
let user = reports["user"] as? String
let status = reports["status"] as? String
let userID = Auth.auth().currentUser?.uid
if user == userID && status == self.pageStatus{
self.reportList.append(firstName! + " " + lastName!)
self.reportsTableView.reloadData()
}
else{
print(status ?? "page status" + self.pageStatus);
}
}
// Do any additional setup after loading the view.
}
)}
答案 0 :(得分:0)
您的问题是所有IBActions 始终都没有回复。
您有很多选择,其中两个是:
pageStatus
,只需设置该值并调用一个使用名为pageStatus
的实例变量的函数。答案 1 :(得分:0)
您不需要在IBAction中退回任何内容,只需更改"让"去" var"使pageStatus可变并在你的函数中设置它:
var pageStatus = "default"
@IBAction func segmentControlPressed(_ sender: UISegmentedControl) {
switch segmentControl.selectedSegmentIndex
{
case 0:
pageStatus = "unclaimed";
case 1:
pageStatus = "complete";
case 2:
pageStatus = "cancelled";
default:
break;
}
}
答案 2 :(得分:0)
确保您的IBAction已连接为ValueChanged而不是TouchUpInside