我正在尝试完成这个应用程序,这是一个表单。填写完成后,稍后将按下一个按钮进行打印。第一个控制器有一个表视图,其中有第1页、第2页、第3页。第1页打开,您可以填写所有信息。当点击保存时,它会带你回到第一个控制器。然后,当您按下“打印”按钮时,将打开表视图并加载所有信息。我正在努力正确地使用结构。在打印控制器页面的委派上,保存按钮也很困难。

我的页面控制器代码
class PagesController: UIViewController, UITableViewDelegate {
var pages = ["Page 1","Page 2","Page 3"]
@IBOutlet weak var pagesTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
pagesTableView.delegate = self
pagesTableView.dataSource = self
}
@IBAction func printBtnPressed(_ sender: Any) {
} }
extension PagesController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pages.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PagesCell")
cell?.textLabel?.text = pages[indexPath.row]
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
performSegue(withIdentifier: "ChildInfoSegue", sender: self)
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 90.0
}
}这是我的表单控制器
protocol ChildDelegate {
func saveInfoBlock(form: ChildInfo)
}
class ChildInfoTableViewController: UITableViewController {
var formDelegate: ChildDelegate!
var firstNameText: UITextField = UITextField()
var lastNameText: UITextField = UITextField()
var middleNameText: UITextField = UITextField()
//MARK: From ChildInfo.swift STRUCT
var childInfo = [ChildInfo]()
var basicChildInfo = ["Child's First Name","Child's Middle Name","Child's Last Name"]
override func viewDidLoad() {
super.viewDidLoad()
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return basicChildInfo.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ChildInfoCell") as! ChildInfoTableViewCell
cell.formLabel.text = basicChildInfo[indexPath.row]
return cell
}
//MARK: Segue to printController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let path = self.tableView.indexPathForSelectedRow!
let destViewController = segue.destination as! PrintPageTableViewController
}
//MARK: Cancel Button Pressed
@IBAction func cancelBtnPressed(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
@IBAction func saveBtnPressed(_ sender: Any) {
let firstname = firstNameText.text ?? ""
let middlename = middleNameText.text ?? ""
let lastname = lastNameText.text ?? ""
let formData = ChildInfo(cFirstName: firstname, cMiddleName: middlename, cLastName: lastname)
self.navigationController?.popViewController(animated: true)
formDelegate.saveInfoBlock(form: formData)
saveInfoBLock()
}
func saveInfoBLock() {
print("Saving Info")
dismiss(animated: true, completion: nil)
}
}下面是我没有正确使用的结构
struct ChildInfo {
var childFirstName: String?
var childMiddleName: String?
var childLastName: String?
var childsMadeUpName: String = ""
init(cFirstName: String, cMiddleName: String, cLastName: String) {
self.childFirstName = cFirstName
self.childMiddleName = cMiddleName
self.childLastName = cLastName
}
}最后是我的tableviewcell
class ChildInfoTableViewCell: UITableViewCell {
@IBOutlet weak var formText: UITextField!
@IBOutlet weak var formLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}一旦委派协议通过。在printtableviewcontroller中放入什么来显示数据?我有人在这里帮助我,但这一切都是编程的,我还没有那么高级。
发布于 2018-01-22 15:55:58
您可以执行以下几项操作:
要使用委托,您必须对委托是如何工作的有一个清晰的概念。
这是一个链接,指向我在另一个网站上给别人的解释。它把它分解了,我想这可能会对你有一点帮助。
https://teamtreehouse.com/community/totally-confused-with-the-example-used
现在,我认为你的问题的解决方案是使用Segues。
当您点击按钮,将您带到您要在其中填充信息的ChildInfoTableViewController时,您将在其中创建"ChildInfo“对象。然后,当您按下save时,您将触发段并将"ChildInfo“对象传递给"PagesController”。
然后,从那里,相同对象应该通过分段进入打印视图。
https://stackoverflow.com/questions/48375559
复制相似问题