[英]protocol delegates in swift to multiple views
我正在尝试学习有关代表的一切。 在此示例中,主页称为PagesTableViewController。 当用户单击页面1时,它将转到DetailsViewController。 填写标签后,您将返回到PagesTableViewController。 然后,当按下Button时,将带您到PrintViewController并显示DetailsViewController中填写的标签。 我一直在尝试教程,并且代码中的某些内容我一定做错了。
这是我的看法
这是PagesTableView的代码
import UIKit
class PagesTableViewController: UIViewController {
let pages = ["Page 1","Page 2","Page 3"]
@IBOutlet weak var pagesTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
pagesTableView.dataSource = self
}
@IBAction func printBtnPressed(_ sender: Any) {
}
}
extension PagesTableViewController: UITableViewDataSource {
// MARK: - Table view data source
func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return pages.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PagesCell", for: indexPath)
cell.textLabel?.text = pages[indexPath.row]
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "DetailsViewSegue" {
}
}
}
DetailsViewController
import UIKit
class DetailsViewController: UIViewController {
@IBOutlet weak var detailsTextField: UITextField!
var childDelegate: basicChildInfo?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func cancelBtnPressed(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
@IBAction func saveBtnPressed(_ sender: Any) {
let data = detailsTextField.text
childDelegate?.userEnteredChildInfo(data: data!)
dismiss(animated: true, completion: nil)
}
}
PrintViewController
import UIKit
class PrintViewController: UIViewController, basicChildInfo {
@IBOutlet weak var printLabelOne: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
//protocol func pulled from child info -- Data from child info
func userEnteredChildInfo(data: String) {
printLabelOne.text = data
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "PrintSegue" {
let childInfoVC: DetailsViewController = segue.destination as! DetailsViewController
childInfoVC.childDelegate = self
}
}
@IBAction func printBtnPressed(_ sender: Any) {
print("Printing File")
dismiss(animated: true, completion: nil)
}
}
最后是我的协议。 快速打开文件
import Foundation
protocol basicChildInfo {
func userEnteredChildInfo(data: String)
}
我的猜测是DetailsViewController
上的委托永远不会被设置。 这是您的UI控制流程的顺序(据我了解):
DetailsViewController
DetailsViewController
通知其委托人有关该文本的信息并关闭自身,从而被删除 PrintViewController
因此,在DetailsViewController
想要将其输入值通知给其委托人时,该委托人尚未设置。 实际上, PrintViewController
甚至不存在。
更糟糕的是,由于没有从Print VC到Details VC的segue,因此很有可能永远不会调用PrintViewController
-segue中的代码,并且仅在发生基于segue的过渡时才运行此方法。
因此,回到您要解决的问题:我建议您...
PagesTableViewController
符合委托协议,并在出现DetailsViewController
时将其设置为委托 PagesTableViewController
的成员变量中 PrintViewController
时,在PrintViewController
的属性上设置存储在该成员var中的PrintViewController
因此,这两项操作-在DetailsViewController
上设置委托并在PrintViewController
上配置输入的文本-进入PrintViewController
“准备进行搜索”:
class PagesTableViewController: UIViewController, BasicChildInfo {
private var userText: String? = nil
func userEnteredChildInfo(data: String) {
userText.text = data
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "DetailsViewSegue" {
(segue.destination as! DetailsViewController).childDelegate = self
}
else if segue.identifier == "PrintSegue" {
(segue.destination as! PrintViewController).userText = self.userText
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.