gpt4 book ai didi

swift - 如何在 UIViewController 之间使用委托(delegate)?

转载 作者:行者123 更新时间:2023-11-30 10:59:03 24 4
gpt4 key购买 nike

我已经搜索过了:

但找不到答案。基本上我有两个 View Controller ,并尝试使用委托(delegate)来更新数组并最终重新加载 Collection View 。

ViewController1

    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)
}
}

我的第二个 View Controller

    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 追加到列表中吗?

我还使用通知中心作为委托(delegate)的替代方案,但没有成功。

最佳答案

您几乎所做的一切都是正确的,但是您将 ViewController2 的委托(delegate)设置错误。

您实际上是在 viewDidLoad 中设置 ViewController2 新实例的委托(delegate),但在 moveToOtherVC 中您呈现了不同的 ViewController2 作为 UINavigationController

的根

而是设置ViewController2新实例的委托(delegate),并使用该实例作为呈现navigationControllerrootViewController

@objc func moveToOtherVC() {
let vc2 = ViewController2()
vc2.delegate = self
let navigationController = UINavigationController(rootViewController: vc2)
present(navigationController, animated: true, completion: nil)
}

或者您只需将 otherView 作为 navigationControllerrootViewController 传递即可解决您的问题

let navigationController = UINavigationController(rootViewController: otherView)

关于swift - 如何在 UIViewController 之间使用委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53614873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com