swift - 如何在 UIViewControllers 之间使用委托?
问题描述
我已经搜遍了:
但找不到答案。基本上我有两个视图控制器,并试图使用委托来更新数组并最终重新加载集合视图。
视图控制器1
class ViewController1: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, AppendDelegate {
let cellID = "cell"
var list = ["item1", "item2", "item3"]
let otherView = ViewController2()
override func viewDidLoad() {
super.viewDidLoad()
otherView.delegate = self
print(list)
let collection = UICollectionView(frame: view.bounds)
collection.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellID)
collection.delegate = self
collection.dataSource = self
collection.backgroundColor = UIColor.cyan
view.backgroundColor = .white
view.addSubview(collection)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(moveToOtherVC))
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as UICollectionViewCell
cell.backgroundColor = UIColor.black
return cell
}
func appendItem(string: String) {
list.append(string)
print(list)
}
@objc func moveToOtherVC() {
let VC2 = UINavigationController(rootViewController: ViewController2())
present(VC2, animated: true, completion: nil)
}
}
我的第二个视图控制器
protocol AppendDelegate {
func appendItem(string: String)
}
class ViewController2: UIViewController {
var delegate: AppendDelegate?
let textField: UITextField = {
let tf = UITextField(frame: CGRect(x: 0, y: 0, width: 39, height: 20))
tf.backgroundColor = .blue
return tf
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
view.addSubview(textField)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(sendDataToCollection))
textField.center.x = view.center.x
textField.center.y = view.center.y
}
@objc func sendDataToCollection() {
guard let text = textField.text else {return}
self.delegate?.appendItem(string: text)
textField.text = ""
moveToVC()
}
@objc func moveToVC() {
dismiss(animated: true, completion: nil)
}
}
在第二个类中,self.delegate.appendItem 不应该导致 ViewController1 附加到列表中吗?
我还使用通知中心作为代表的替代方案,但没有找到运气。
解决方案
像这样使用 ViewController1 函数-
@objc func moveToOtherVC() {
let otherView =
self.storyboard?.instantiateViewController(withIdentifier:
"ViewController2") as! ViewController2
otherView.delegate = self
self.present(next, animated: true, completion: nil)
}
推荐阅读
- angular - 使用 Angular HostBing 添加类而不擦除现有类
- debugging - 使用 Blackmagic probe 和 platformIO undefinedError 进行调试
- python - 如何在python中迭代列表中的项目?
- python - 基于python中元素出现的字符串裁剪
- flutter-web - flutter-web:如何在flutter web中制作悬停菜单?
- mysql - MySQL 查询运行时间更好,即使它的执行计划很糟糕
- json - Angular/Http“put”方法只返回未定义?
- ajax - 从服务器获取响应,但 .html 没有在 wordpress ajax 中播种任何内容
- ios - 使用 Firebase Auth,想要使用 JWT 来验证发送 MongoDB 请求的客户端(iOS 应用程序)
- android - 如何从 Java 代码中检查视图(按钮、ImageView 等)是否可点击?