简体   繁体   中英

Building UI Elements without the storyboard Swift 4

I have just recently decided to transition from building UI elements using the storyboard to building them programmatically.

While looking up tutorials online, I saw some people liked to create their UIs using closures.

For example: let label: UILabel = { // they would customize their UI elements between the brackets }

I was wondering if this was some sort of conventional way used to build UI Elements ?

So far I've been sticking up with declaring my variables(including UI elements declaration) before the viewDidLoad method and declaring functions to configure them below it before calling them in the same 'viewDidLoad' method.

Is it wrong to do it this way?

There is nothing wrong in the way which you do, but using closures, is a common practice in iOS programming. Closures have multiple benefits such as all the view related configuration code is placed in a single place, the code is readable, and it becomes straightforward to make changes to the code and debug if there are any bugs present.

It's about preference. But, in the way you described with closures, you won't be able to access other properties even if you declare them before the property inside which you need them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM