切换视图控制器时变量意外重置为 0
Variable unexpectedly resetting to 0 when View Controller Switched
我在名为 DetailViewController 的 ViewController 中有一个名为“results”的变量,它被赋予了一个值,并在 FirstViewController:
中正确打印
extension FirstViewController: UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath) as! PostTableViewCell
cell.typeLabel.text = "Name: \(posts[indexPath.row].typeOfFood)"
cell.titleLabel.text = "Title: \(posts[indexPath.row].title)"
cell.locationLabel.text = "Location: \(posts[indexPath.row].location)"
cell.nameLabel.text = "Name: \(posts[indexPath.row].name)"
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// print("Select Row \(indexPath.row)")
var result = indexPath.row
tableView.deselectRow(at: indexPath, animated: true)
// self.performSegue(withIdentifier: "showDetail", sender: nil)
let mainStoryBoard = UIStoryboard(name: "Main", bundle: Bundle.main)
guard let detailViewController = mainStoryBoard.instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController else{
print("couldnt find vc")
return
}
let detailVC = DetailViewController()
detailVC.result = indexPath.row
print(detailVC.result)
self.present(detailViewController, animated: true, completion: nil)
}
}
但是当我的应用程序切换到另一个名为 DetailViewController 的 ViewController 时,变量结果始终打印并存储为 0。DetailViewController:
import UIKit
import Firebase
import FirebaseDatabase
class DetailViewController: UIViewController {
@IBOutlet weak var backButton: UIBarButtonItem!
@IBOutlet weak var nameOfUserLabel: UILabel!
@IBOutlet weak var typeOfFoodNeededLabel: UILabel!
@IBOutlet weak var amountOfFoodNeededLabel: UILabel!
@IBOutlet weak var reasonNeededLabel: UILabel!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var locationLabel: UILabel!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var messageButton: UIButton!
var posts = [Posts]()
var rowNumber = String()
var result = Int()
override func viewDidLoad() {
super.viewDidLoad()
var rowNumber = Int()
loadPosts()
print(result)
// nameOfUserLabel.text = posts[result].name
}
func loadPosts() {
Database.database().reference().child("Posts").observe(.childAdded){(snapshot: DataSnapshot) in
if let dict = snapshot.value as? [String: Any]{
let titleText = dict["Title"] as! String
let typeofFoodText = dict["Type of Food"] as! String
let descriptionText = dict["Description"] as! String
let amountNeededText = dict["Number of Items Needed"] as! String
let locationText = dict["Location"] as! String
let nameText = dict["Name of User"] as! String
let reasonNeeded = dict["Reason for Donation"] as! String
let post = Posts(titleText: titleText, typeOfFoodText: typeofFoodText, descriptionText: descriptionText, amountNeededText: amountNeededText, locationText: locationText, nameText: nameText, reasonNeededText: reasonNeeded)
self.posts.append(post)
print(self.posts)
self.nameOfUserLabel.text = self.posts[self.result].name
self.descriptionLabel.text = self.posts[self.result].description
self.typeOfFoodNeededLabel.text = self.posts[self.result].typeOfFood
self.amountOfFoodNeededLabel.text = self.posts[self.result].amountNeeded
self.reasonNeededLabel.text = self.posts[self.result].reasonNeeded
self.locationLabel.text = self.posts[self.result].location
}
}
}
```
我该如何解决这个问题?我主要希望将 FirstViewController 中给出的值传递给 DetailViewController,但它一直重置回 0.
您正在将结果变量传递给新实例 detailVC (detailVC.result),而传递变量的正确实例是 detailViewController (detailViewController.result).
请在您的共享源代码中替换以下内容:
这个
let mainStoryBoard = UIStoryboard(name: "Main", bundle: Bundle.main)
guard let detailViewController = mainStoryBoard.instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController else{
print("couldnt find vc")
return
}
let detailVC = DetailViewController()
detailVC.result = indexPath.row
print(detailVC.result)
self.present(detailViewController, animated: true, completion: nil)
与
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
guard let detailViewController = mainStoryBoard.instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController else{
print("couldnt find vc")
return
}
detailViewController.result = indexPath.row
print(detailViewController.result)
self.present(detailViewController, animated: true, completion: nil)
我在名为 DetailViewController 的 ViewController 中有一个名为“results”的变量,它被赋予了一个值,并在 FirstViewController:
中正确打印extension FirstViewController: UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath) as! PostTableViewCell
cell.typeLabel.text = "Name: \(posts[indexPath.row].typeOfFood)"
cell.titleLabel.text = "Title: \(posts[indexPath.row].title)"
cell.locationLabel.text = "Location: \(posts[indexPath.row].location)"
cell.nameLabel.text = "Name: \(posts[indexPath.row].name)"
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// print("Select Row \(indexPath.row)")
var result = indexPath.row
tableView.deselectRow(at: indexPath, animated: true)
// self.performSegue(withIdentifier: "showDetail", sender: nil)
let mainStoryBoard = UIStoryboard(name: "Main", bundle: Bundle.main)
guard let detailViewController = mainStoryBoard.instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController else{
print("couldnt find vc")
return
}
let detailVC = DetailViewController()
detailVC.result = indexPath.row
print(detailVC.result)
self.present(detailViewController, animated: true, completion: nil)
}
}
但是当我的应用程序切换到另一个名为 DetailViewController 的 ViewController 时,变量结果始终打印并存储为 0。DetailViewController:
import UIKit
import Firebase
import FirebaseDatabase
class DetailViewController: UIViewController {
@IBOutlet weak var backButton: UIBarButtonItem!
@IBOutlet weak var nameOfUserLabel: UILabel!
@IBOutlet weak var typeOfFoodNeededLabel: UILabel!
@IBOutlet weak var amountOfFoodNeededLabel: UILabel!
@IBOutlet weak var reasonNeededLabel: UILabel!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var locationLabel: UILabel!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var messageButton: UIButton!
var posts = [Posts]()
var rowNumber = String()
var result = Int()
override func viewDidLoad() {
super.viewDidLoad()
var rowNumber = Int()
loadPosts()
print(result)
// nameOfUserLabel.text = posts[result].name
}
func loadPosts() {
Database.database().reference().child("Posts").observe(.childAdded){(snapshot: DataSnapshot) in
if let dict = snapshot.value as? [String: Any]{
let titleText = dict["Title"] as! String
let typeofFoodText = dict["Type of Food"] as! String
let descriptionText = dict["Description"] as! String
let amountNeededText = dict["Number of Items Needed"] as! String
let locationText = dict["Location"] as! String
let nameText = dict["Name of User"] as! String
let reasonNeeded = dict["Reason for Donation"] as! String
let post = Posts(titleText: titleText, typeOfFoodText: typeofFoodText, descriptionText: descriptionText, amountNeededText: amountNeededText, locationText: locationText, nameText: nameText, reasonNeededText: reasonNeeded)
self.posts.append(post)
print(self.posts)
self.nameOfUserLabel.text = self.posts[self.result].name
self.descriptionLabel.text = self.posts[self.result].description
self.typeOfFoodNeededLabel.text = self.posts[self.result].typeOfFood
self.amountOfFoodNeededLabel.text = self.posts[self.result].amountNeeded
self.reasonNeededLabel.text = self.posts[self.result].reasonNeeded
self.locationLabel.text = self.posts[self.result].location
}
}
}
```
我该如何解决这个问题?我主要希望将 FirstViewController 中给出的值传递给 DetailViewController,但它一直重置回 0.
您正在将结果变量传递给新实例 detailVC (detailVC.result),而传递变量的正确实例是 detailViewController (detailViewController.result).
请在您的共享源代码中替换以下内容:
这个
let mainStoryBoard = UIStoryboard(name: "Main", bundle: Bundle.main)
guard let detailViewController = mainStoryBoard.instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController else{
print("couldnt find vc")
return
}
let detailVC = DetailViewController()
detailVC.result = indexPath.row
print(detailVC.result)
self.present(detailViewController, animated: true, completion: nil)
与
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
guard let detailViewController = mainStoryBoard.instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController else{
print("couldnt find vc")
return
}
detailViewController.result = indexPath.row
print(detailViewController.result)
self.present(detailViewController, animated: true, completion: nil)