Sending our Company Back through Delegation
Intermediate Training Core Data
After a company is created, we need to send it back to our main list. Doing this requires a connection to be made between our list and the creation form, this connection is what we call delegation. In order to successfully know about the newly created object, we pass it back through a custom Protocol method call. Upon success, we can update the UITableView by inserting a new row using an IndexPath.

Comments (30)
alpertabak
5 years ago
So far so good.
Brian Voong
5 years ago
Mike
5 years ago
I always watch your YouTube videos at 1.5x speed. A similar option would be great here too.
Brian Voong
5 years ago
mcordero
5 years ago
If you already have experience with iOS development, it's easier to follow along w/o having to stop and understand as much.
Brian Voong
5 years ago
Sylvain
5 years ago
Hello, I don't understand, why do we use the optional "var companiesController: CompaniesController?" instead of an instance like "var companiesController = CompaniesController( )" ? I've tried to use an instance and I had a problem with the number of row. But I still doesn't understand how it works. Thanks
Brian Voong
5 years ago
Sylvain
5 years ago
Thank you Brian ! I finally understand the difference. I couldn't find anything on internet and as a french, my english is not so sharp.
Brian Voong
5 years ago
dwendel10
5 years ago
Hello, I have a question, if i have 5 viewcontrollers and want the last one to have the data from the first how would this be accomplished? I can do it just fine the way we did it in this video but when i try to do it to another vc doesn't not pass. Any feedback is appreciated! Thanks!
dwendel10
5 years ago
**Revised Question** Hello, I have a question, if i have 5 viewControllers and want the last viewController to have the data from the first how would this be accomplished? I can do it just fine passing data from the first viewController to the second, but passing from the first to the last does not work. Any feedback is appreciated! Thanks!
Brian Voong
5 years ago
rcao140
5 years ago
Instead of adding company name to the bottom of the list, how can we add it to the top of the list? Thank you.
Brian Voong
5 years ago
rcao140
5 years ago
It still added it to the bottom. I modified the code as follows: func addCompany(company: Company){ companies.insert(company, at: 0) let newIndexPath = IndexPath(row: 0, section: 0) tableView.insertRows(at: [newIndexPath], with: .automatic) }
rcao140
5 years ago
Never mind, I changed func didAddCompany(company: Company) to match the above method, it works now! Thanks!
Daniel Peach
5 years ago
Love how he says "Lets wrap up this lesson" halfway through the video lol
dclawson
5 years ago
Yeah, as soon as he said that, I went to see the time remaining. Classic.
Daniel Peach
5 years ago
Wow well that is a problem... Another website problem discovered: When a user submits a question comment while watching the video, the whole page reloads and resets the user to the beginning of the video.
Juanpablo Macias
5 years ago
Beautiful lesson. Very grateful to you for showing us the way!
Prabhdeep Singh Randhawa
5 years ago
Hi brian, Thanks for sharing such precious information. Just got a question,that for slide in menus i m still using old way of cgrect(x,y,width,height).which never work well in landscape mode.can i use these constraints in slide in menu.can u pls give a short example. Thanks in advance
Brian Voong
5 years ago
xinlok
5 years ago
Hi Brian, Thanks for your great Course, I have a question: how to get ride of this message: "if we're in the real pre-commit handler we can't actually add any new fences due to CA restriction" Thanks and Regards
mohitnandwani
5 years ago
Hey Brian! I recently bought the course and going through all these awesome tutorials. Can you please explain the difference between the weak protocol reference vs the normal one? And when to use weak protocol. Thanks.
Nikhil Pandey
5 years ago
Hey Brian, I am trying to build a UItextField for data entry and a UIPickerView for users to pick out of some options for classification. Have you got any idea how to implement it inside a custom Collectionview cell and how to use delegation to get user input into my model.
cesare
5 years ago
why didn't you reload the table view data instead of inserting a new index path manually? i wonder what the difference is
Brian Voong
5 years ago
magic
5 years ago
Hey Brain, Thanks for the great tutorial!! I have one little question, why didn't you use leading and trailing constraints instead of right and left constraints ?
Brian Voong
5 years ago
magic
5 years ago
Oh i see :) Thanks for the reply
DiegoOruna
5 years ago
Hi Brian. For some reason, when I put the anchor view.topAnchor, it is taking me as if the view started from the status bar, and not from where the navigation bar ends. What is this about. already check the code and it's the same
DiegoOruna
5 years ago
Oh no, I found the answer after an hour haha, it was nonsense. had not set the property IsTranslucent = False. I did not know that this property makes the NavBar "transparent", I thought it only affected the color. It's been a pretty interesting mistake
Brian Voong
5 years ago
bfriendjr
5 years ago
I already completed the course as instructed in the videos. To challenge my skills, I am now going thru and re-taking the course and using the Interface Builder instead of coding the interface. All was going well up to this session. I can not seem to get the new record info created on the "CreateRVController.swift" back to the TableView on "RVsController". The "Test Add" button on the "RVsController" works just fine and the TableView added the new row as expected. It would be greatly appreciated if someone could look at my code and help me figure out what I am missing or doing wrong. Here is a link to the latest version of the code. Thanks in advance..... https://github.com/bfriendjr/InterTrainingRV
Alen Kirm
5 years ago
Nice series, looking forward to complete it since in the job, we are also using Core Data for our app (Loop - email messenger). I'd like to point out, that it is almost necessary to mark our delegate property as a weak (Inside CreateCompanyController - weak var delegate : CreateCompanyControllerDelegate?) so we avoid possible memory leak. This means, relationship between CreateCompanyController and CompaniesController is freed, in our case, when CreateCompanyController dismisses. In case you want to mark delegate as a weak, you should also constraint CreateCompanyControllerDelegate to a class, since classes are reference types (protocol CreateCompanyControllerDelegate: class). Hope this helps you :)
seekerlk
5 years ago
view.topAnchor didn't work for me. I had to go like below. nameLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true I think it's part of new iOS framwork change that was not existed before :)
RichyCode
5 years ago
yep you are correct good spot
xuhua
4 years ago
thanks for this tip! you are right!
Haohong Zhao
4 years ago
When I change back from landscape view to normal view, the navigation bar title: "Create Company" will always go to the middle, and then, if I click cancel and re-open the "create company view", the title will be on the left again. Is this a problem related to the simulator, or we have to set up some property to fix this issue?
Brian Voong
4 years ago
jpalacios
4 years ago
Is it common for companies to not use the storyboard for UI creation?
Brian Voong
4 years ago
werg09
4 years ago
This is a MONSTER tutorial! Great stuff!
Cinquain
4 years ago
This video was truly breathtaking Brian. Omg you are so talented at teaching. I wish to become like you one day!
jdhindsa
4 years ago
Unfortunately you lost me with the Auto Layout in code. Are there any resources you used to learn this?
LeonWB
4 years ago
When I tried to put in UI elements on the CreateCompany View, they did not show using view.TopAnchor etc because they were hidden behind the Navigation Bar. It looks like view.topAnchor is the view of the frame. From ios 11 onward, there is now view.SafeAreaLayoutGuide which provides a view which is unobstructed by any Status Bar, Navigation Bar and Tab Bar. Therefore, in your example, use: guide = view.SafeAreaLayoutGuide let label = UILabel() label.text = "Name" label.backgroundColor = .red label.textColor = .black label.translatesAutoresizingMaskIntoConstrints = false view.addSubview(label) label.topAnchor.constraint(equalTo: guide.topAnchor).isActive = true label.leftAnchor.constraint(equalTo: guide.leftAnchor).isActive = true label.rightAnchor.constraint(equalTo: guide.rightAnchor).isActive = true label.heightAnchor.constraint(equalToConstant: 50).isActive = true That works a treat, and is usable on all UIViews whether or not there are navigation bars etc.
Gunjan Goel
4 years ago
Hey Brian , While entering the text in textField when we forced unwrap that you mentioned it was already having the empty String. Can you please tell where are we setting it as Empty ?
Brian Voong
4 years ago
Cinquain
4 years ago
Great lesson, that video was lit
Brian Voong
4 years ago
Cinquain
4 years ago
You gotta study the master
dwendel10
3 years ago
Hey Brian for some reason my ddiAddCompany method is not getting called when clicking the save button. I am not sure why this is happening. Could you take a look and see if i am missing something? @objc func handleSave(){ print("Trying to save company") dismiss(animated: true) { guard let name = self.nameTextField.text else {return} let company = Company(name: name, founded: Date()) self.delegate?.didAddCompany(company: company) } } @objc func handleAddCompany(){ print("Adding companies") let createCompanyController = CreateCompanyController() let navController = CustomNavigationController(rootViewController: CreateCompanyController()) navController.view.backgroundColor = .lightRed navController.modalPresentationStyle = .fullScreen createCompanyController.delegate = self present(navController, animated: true, completion: nil) }
dwendel10
3 years ago
Never mind I figured to out!
Deonte
3 years ago
Thank you so much for taking the time to explain Delegates in this video, it really helped me understand the concept.
JaimeHe
3 years ago
Hey Brain, I meet a UI Layout issue about adding namelabel to the createCompany view, I have downloaded your project and founded that it works. But according to my UIWindowScene diagram which is in the Xcode, the namelabel has added to the position where is near to the screen's top edge and it is covered by the navigation bar. Should I set a fixed value for the top anchor or do you have other solutions? I have tried to use [navigationController!.navigationBar.bottomAnchor] to replace the original one, but simulator crash and Xcode tell me that view hierarchies are illegal
Brian Voong
3 years ago
JaimeHe
3 years ago
It works. Thanks a lot.
Yamak
3 years ago
Hi Brian, the modal acts differently in Xcode 11. They look different as well. The safeAreaLayoutGuide solved the problem. I also noticed that you have a YouTube video which illustrate how to get the "full screen" modal. I'll have a look at that when I get a little more advanced. Thanks!
Brian Voong
3 years ago
Yamak
3 years ago
Perfect, that works! Also, just FYI, noticed that if .overCurrentContext is used instead of .fullsceen, the status bar retains the light appearance (seems that .fullscreen makes the status bar revert back to the dark/black appearance). Less code to write, right!
arroyot24
3 years ago
Hello Brian. Everything OK with the lessons until now, except for the fact that I can't make CreateCompanyController nameLabel and textField subviews appear. I've double-checked the code, even copying it complete from yours, but that subview doesn't appear on the controller view. I've as well checked the configuration on AppDelegate and SceneDelegate, as it were working OK on previous lessons. Any suggestion? I'm eager to start with Core Data section and don't want to go on without having everything up and running. Thanks!
arroyot24
3 years ago
I've find the solution in the comments. Apple's changes on swift and Xcode makes course out-of date so soon... Now using Xcode 11.7 and Swift 5. I've modified this two values on CreateCompanyController: lightBlueBackgroundView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true nameLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true Efectively, it seems that NavBar was hiding label view. Nos working OK. Going to Core Data section ;-)
HELP & SUPPORT