Delegates & Protocols - The Messy Way

Page 643 of the ePub, I am unable to add item using the messy way and could not figure it out.

@gdelarosa In looking at my code I do not have the ‘add’ func in my ChecklistViewController. What would the function be written like so that my done button (in the AddItemViewController) works? Thanks.

See code attached.Screen Shot 2020-09-29 at 9.55.50 AM

Hi @geezer, which version are you using for iOS apprentice? I’m looking at Version 8.3.0 (8th Edition). It appears that they are using SwiftUI and from your screenshot it appears that you are using UIKit. Also, please let us know what chapter you are referring too. For some reason I am unable to view page numbers with the ePub version.

Best,
Gina

@gdelarosa I am using iOS apprentice Eighth Edition (Chapter 13 - Delegates & Protocols). My apologies, the ePub is actually pages 689 and 690. In the PDF it is page 329. I have not tackled any SwiftUI at this stage only Swift. I hope this is not too pushy, I am just trying to move ahead in chapter and that’s tough to do when I can’t figure out the opening exercise correctly (adding an item the messy way). CC @syedfa

Hi @geezer, thanks for letting us know the edition. Could you clarify which version you are using so I can download it and go the the chapter you’re on please?

Screen Shot 2020-09-30 at 8.16.56 AM

The May 2020 edition (v8.3.0)

@geezer, I would change the variable checklistViewController to be a reference type to ChecklistViewController. So instead of var checklistViewController: ChecklistViewController I would do
var checklistViewController = ChecklistViewController()
That should remove the first error since we are then just creating an instance of the ChecklistViewController. As for the exercise, I would start by creating a function called add in the ChecklistViewController since your error is expecting “add” somewhere in the view controller. Maybe something like,

   func add(_:ChecklistItem) {
    
   }

Then if we go back to AddItemViewController, we should hopefully have the error resolved.

This gets rid of the errors, however, if I enter text on the textfield and click done that text is not shown on the table on my initial viewcontroller.

this is what my code looks like in the 2 view controllers
first viewcontroller
func addItem(_:Checklistitem) {
}

second viewcontroller
@IBAction func done() {
let item = Checklistitem()
item.text = textField.text!
CHECKVIEW.addItem(item)
navigationController?.popViewController(animated: true)
}

** My problem at moment is that I am not seeing what I enter in the textfield when my initial viewcontroller loads. I don’t understand how to append it to the array so that I can see it.

@geezer I don’t have an answer for the “messy way” exercise that you’re referring to. The code above won’t be in the “final” version of this chapter.
I also wouldn’t worry too much since the chapter focuses on delegates & protocols which is a much better way of communicating between two view controllers and I’m pretty sure your code will change as you go along the chapter.

This topic was automatically closed after 166 days. New replies are no longer allowed.