我试图将字符串从属性列表传递到UiTextView。虽然我已经准备好代码,并且正在设法用它填充TabelViewCell,然后将数据传递给UiViewController,但是我无法将数据从该UiViewController传递给另一个具有UiTextView的UiViewController。 有人可以解释我该怎么做吗?
这是我用来填充tabelViewCell和下一个UiViewController的代码:
import UIKit
struct cellData{
var image : UIImage?
var message : String?
}
class TableViewController: UITableViewController {
var items = [[String:String]]()
func loadPlist() -> [[String:String]] {
let path = Bundle.main.path(forResource: "Property List", ofType: ".plist")
return NSArray.init(contentsOf: URL(fileURLWithPath: path!)) as! [[String:String]]
}
override func viewDidLoad() {
super.viewDidLoad()
self.items = loadPlist()
self.tableView.register(RestauarntTabelViewCell.self, forCellReuseIdentifier: "Cell")
tableView.target(forAction: #selector(detaliiRestaurant), withSender: nil)
navigationItem.title = "Restaurants"
self.navigationItem.setRightBarButton(UIBarButtonItem(title: "Find Us", style: .plain, target: self, action: #selector(detaliiRestaurant)), animated: true)
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 200
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell") as! RestauarntTabelViewCell
let item = self.items[indexPath.row]
cell.image1View.image = UIImage(named: item["Image"]!)
cell.messageView.text = item["Adress"]
cell.titleView.text = item["Name"]
cell.layoutSubviews()
cell.target(forAction: #selector(detaliiRestaurant), withSender: self)
return cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let restDet = RestaurantDetails()
let item = self.items[indexPath.row]
restDet.sentData1 = item["Name"]
restDet.sentData2 = item["Adress"]
restDet.sentData3 = item["Image"]
restDet.sentData4 = item["Description"]
let restMenu = RestaurantMenuVC()
restMenu.sentData10 = item["Food Menu"]
self.navigationController?.pushViewController(restDet, animated: true)
}
@objc func detaliiRestaurant() {
let restDet = RestaurantDetails()
navigationController?.pushViewController(restDet, animated: true)
}
}
然后,我想从我在第一个TabelViewController上调用的dat数组和属性列表中填充此UViewController和UIViewText。这是代码:
import UIKit
class RestaurantMenuVC: UIViewController {
var sentData10 : String!
var sentData1 : String!
var sentData2 : String!
var sentData3 : String!
var sentData4 : String!
var sentData5 : String!
var sentData6 : String!
var textDetails1 : UITextView = {
var textViewDetails1 = UITextView()
textViewDetails1.translatesAutoresizingMaskIntoConstraints = false
textViewDetails1.allowsEditingTextAttributes = false
textViewDetails1.font = UIFont.boldSystemFont(ofSize: 30)
textViewDetails1.textColor = UIColor.black
textViewDetails1.backgroundColor = UIColor.clear
return textViewDetails1
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
imagineFundal()
navigationItem.rightBarButtonItem?.title = "Order table"
self.textDetails1.text = sentData4
constraintsTextView()
}
func imagineFundal() {
let imagine = UIImage(named: "johny-goerend-403554-unsplash.jpg")
var poza : UIImageView
poza = UIImageView(frame: view.bounds)
poza.clipsToBounds = true
poza.contentMode = UIView.ContentMode.scaleAspectFill
poza.image = imagine
poza.center = view.center
view.addSubview(poza)
self.view.sendSubviewToBack(poza)
}
func constraintsTextView() {
let stackView = UIStackView(arrangedSubviews: [textDetails1])
stackView.axis = .vertical
stackView.spacing = 50
stackView.distribution = .fillEqually
view.addSubview(stackView)
stackView.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingtop: 100, paddingleft: 10, paddingbottom: 0, paddingright: 10, widht: 0, height: 650)
}
}