protocol NewHomeVCDelegate {
func didEditHabit(_ habit: Habit)
}
class NewHomeVC: {
var newHomeVCDelegate : NewHomeVCDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
func didButtonTapped() {
self.newHomeVCDelegate?.didEditHabit(habit)
let vc = NewCreateHabitVC()
vc.modalPresentationStyle = .popover
self.present(vc, animated: true)
}
}
class NewCreateHabitVC: NewHomeVCDelegate {
func didEditHabit(_ habit: Habit) {
updateUI(habit: habit)
}
override func viewDidLoad() {
super.viewDidLoad()
setViews()
setDelegates()
addGestureRecognizer()
var newHomeVC = NewHomeVC()
newHomeVC.newHomeVCDelegate = self
}
func updateUI(habit: Habit) {
// it does some stuff here
}
}
我正在尝试通过委托和协议将习惯对象从 NewHomeVC 传递到 NewCreateHabitVC。但是问题是代表不听。有人可以告诉我我在这里做错了什么吗?
我相信你让这更复杂了。 在这里,您需要根据您的代码通过委托将习惯对象从 NewHomeVC 传递到 NewCreateHabitVC,它会将数据发送到前屏 vc,我看不到使用它的意义。
NewHomeVC
protocol NewHomeVCDelegate {
func didEditHabit(_ habit: String)
}
class NewHomeVC: UIViewController {
let label : UILabel = {
let label = UILabel()
label.text = "Hello world"
label.isUserInteractionEnabled = true
return label
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
view.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
label.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
label.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
label.addGestureRecognizer( UITapGestureRecognizer(target: self, action: #selector(tapFunction)))
}
@objc
func tapFunction(sender:UITapGestureRecognizer) {
didButtonTapped()
}
func didButtonTapped() {
//you put it up here && wrong way of init the files
let vc = NewCreateHabitVC()
vc.modalPresentationStyle = .popover
self.present(vc, animated: true)
vc.newHomeVCDelegate?.didEditHabit("asdfsdfsdfsdfsdfs")
}
}
NewCreateHabitVC
class NewCreateHabitVC: UIViewController {
let label : UILabel = {
let label = UILabel()
label.text = "Hello world"
return label
}()
var newHomeVCDelegate : NewHomeVCDelegate?
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
self.setConstraint()
newHomeVCDelegate = self
}
}
extension NewCreateHabitVC : NewHomeVCDelegate {
func didEditHabit(_ habit: String) {
print(habit)
}
}
extension NewCreateHabitVC {
func setConstraint() {
view.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
label.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
label.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
}
对于有协议/完成的代表。它更像是将数据向后发送到呼叫者屏幕。 传递到下一个屏幕 vc 需要什么:
NewHomeVC
let vc = NewCreateHabitVC()
vc.habbit = yourdata
vc.modalPresentationStyle = .popover
self.present(vc, animated: true)
NewCreateHabitVC
class NewCreateHabitVC : UIViewController {
.....
var habbit : your_data_type? = nil
......
}
struct Habit {
let name : String
enum CodingKeys: String, CodingKey {
case name = "name"
}
}
protocol NewHomeVCDelegate {
func didEditHabit(_ habit: Habit)}
class NewHomeVC: UIViewController , NewHomeVCDelegate{
//var newHomeVCDelegate : NewHomeVCDelegate?
let myHabit = Habit(name: "I want to edit this habit")
override func viewDidLoad() {
super.viewDidLoad()
}
func didButtonTapped() {
//no need here
//self.newHomeVCDelegate?.didEditHabit(habit)
//when you present the ViewController you the set the delegate, so he can ping you back once done. Also you need to set the object that you intend to edit
let vc = NewCreateHabitVC()
vc.newHomeVCDelegate = self
vc.habitThatIwantToEdit = myHabit
vc.modalPresentationStyle = .popover
self.present(vc, animated: true)
}
//you have to implement this method in home class
func didEditHabit(_ habit: Habit) {
//this object is the final edited object from NewCreateHabitVC, this will be called on finish button press, here you have it.
}
}
class NewCreateHabitVC: UIViewController, NewHomeVCDelegate {
//but here
var newHomeVCDelegate : NewHomeVCDelegate?
var habitThatIwantToEdit :Habit?
func didEditHabit(_ habit: Habit) {
//
//updateUI(habit: habit)
}
override func viewDidLoad() {
super.viewDidLoad()
//setViews()
//setDelegates()
//addGestureRecognizer()
//var newHomeVC = NewHomeVC()
//newHomeVC.newHomeVCDelegate = self
if let _ = habitThatIwantToEdit{
updateUI(habit: habitThatIwantToEdit!)
}else{
print("object not set")
}
}
func updateUI(habit: Habit) {
// it does some stuff here
}
func finishButtonTaped(){
if let habit = habitThatIwantToEdit{
newHomeVCDelegate?.didEditHabit(habit)
}
}
}