Employees UI and Useful Extensions
Intermediate Training Core Data
With our Companies screens mostly complete, we're ready to move onto Employees. In this lesson, we'll layout the foundation that holds all of our employees and also build the Employee creation screen. While building out these new screens, I want to go through using an extension on UIViewController that allows us to share code throughout our app. Let's start coding!

Comments (9)
Mike
5 years ago
These extension methods are really helpful when you want to have a clutter free ViewController. Nice trick.
Brian Voong
5 years ago
cyn89110
5 years ago
Hey Brian, 我希望用中文能更好表達我的意思,我知道 present 跟 push 的差別, 但我不知道他們使用的恰當時機為何,可以幫我解惑一下嗎?,謝謝。 歐對了,我來自台灣~很高興知道你有到台灣旅遊!
Brian Voong
5 years ago
xuhua
4 years ago
这个解答我给满分
jacquesj
5 years ago
Hello Brian! I was wondering why you put the navigationItem.title = company?.name in the viewWillAppear instead of the viewDidLoad? I moved it to the viewDidLoad and it seems to be the same.
Brian Voong
5 years ago
nerddd
5 years ago
Also you can set it like that: EmployeesController var company: Company ? { didSet { guard companyName = company.name else { return } title = companyName } } In my case it is working well.
nerddd
5 years ago
guard let companyName *
andreas.riexinger@gmail.com
5 years ago
Hey Brian, can you explain the difference between pushViewController and popViewController? Why are you "pushing" the ViewController instead of something else?
Brian Voong
5 years ago
andreas.riexinger@gmail.com
5 years ago
Hi Brian, Thanks a lot for your quick answer yesterday. Cheers, Andreas
Uphopho
4 years ago
Thanks, I was wondering about this as well, and couldn't really find a clear answer online.
xinlok
5 years ago
Hey Brian, Thank you for this great course. I need your help with one issue, I want to add an UILabel to a cell, but to avoid reuse cell I add: cell.layer.sublayers?.forEach { $0.removeFromSuperlayer() }. one I run it, it gave error. Can you please let me know what to do in order to avoid the reuse cell?
Brian Voong
5 years ago
xinlok
5 years ago
Brian, In order to make my question more clear, by looking to your youtube video "Expandable and Collapsible Sections UITableView (Ep 3)", I follow the same by instead using "cell.textLabel?.text = name", I add the following: let label = UILabel() cell.contentView.addSubview(label) //cell.contentView.addSubview(label) label.translatesAutoresizingMaskIntoConstraints = false label.leftAnchor.constraint(equalTo: cell.leftAnchor).isActive = true label.topAnchor.constraint(equalTo: cell.topAnchor).isActive = true label.widthAnchor.constraint(equalTo: cell.widthAnchor).isActive = true label.heightAnchor.constraint(equalToConstant: 30).isActive = true label.text = name I know this is out of this course, but for you solving this problem it is a matter of minutes.
RichyCode
5 years ago
loved this lesson with the extension of UiViewController and whacking those buttons in there for use throughout the project....awsome stuff thanks Brian
Prabhdeep Singh Randhawa
5 years ago
hi Bryan, for navigation title I used this code instead of view will appear in employesscontroller is used override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let vc = EmployeesController() let company = companies[indexPath.row] vc.navigationItem.title = company.name show(vc, sender: nil) } this give me the required result .so is that also the right way to do
Tube
5 years ago
It seems to me that after going to the trouble to "refactor" a controller into separate extension files, one ends up with a listing just like the free one Xcode provides in the selection menu at the top of the editor pane. And, despite the fashion, I cannot see that having the controller code in separate files is easier to read or maintain. Further, it is not the purpose of extensions to reduce the size of source files. I know I am swimming upstream here, but a single, well-documented, and well-organized source file is much easier to maintain than a dozen undocumented partial source files.
dclawson
5 years ago
Just an idea from a beginner: maybe it does seem pointless to do this now, but let's say you had to setup four or five UI elements for many view controllers. It would be nice to have them all in once place if you had to change them later.
RichyCode
4 years ago
coudlnt disagree more with everything you have said here, if you love large view controllers.... just skip on no harm done
Christopher J. Roura
3 years ago
Tube, I see your point but think there are a few points worth mentioning. This is an app made by a singular person. You know what you're doing and you understand why. When something doesn't work odds are you know what you did and know what to do to fix it. When multiplying this to a team of even 5-10 people (very small team) it can still very easily be documented if the people actually keep their comments up to date. However, when you have a train of thought and know how you want to accomplish a big grant task a comment doesn't always get that point across. Usually when working with other people this becomes an issue. Not only do people have certain code preferences but people also have certain code maintainability preferences. I've been on both sides where I have had people code in different styles and even with commenting it is still very confusing because they have different intentions with how they want to structure and maintain their apps. Placing files separately still allows for structure and maintainability but also allows for any coder to be able to easily pick up and find out what is being done.
jdhindsa
4 years ago
Brian, wouldn't the extension need to be on UITableViewController instead of UIViewController? Or did you do it this way because UIViewController is a superclass of UITableViewController?
Brian Voong
4 years ago
HELP & SUPPORT