Where to build views programmatically using a tip

I searched a lot and made my way through a couple of text books, but I would really appreciate this simple explanation of best practice for defining UIView subclasses in an iOS app.

  • If you are using xib, where can I add / customize controls at the start of execution?
  • If you are programming programmatically, should I do this in a ViewController (loadView?) Or a separate subclass of UIView? If the latter, how can I specify his file hosting service, so that if it is added as a subtitle, he knows who his controller is?
  • What are you doing in awakeFromNib, loadView, viewDidLoad, UIView.init, UIViewController.init, etc.
  • What other methods do you often use?

I don’t need specific instructions beyond this - I’m looking for a quick reference guide that explains what code is designed to live in each of the available methods.

+6
source share
3 answers

My practice is this:

  • create a separate UIView subclass in a separate file.
  • in UIViewController init to create all objects that are not related to the user interface
  • in a UIViewController loadView create my custom UIView and set it as a self.view controller
  • in a UIViewController viewDidUnload (called upon a memory alert) frees all UI components (my user UIView)
  • in UIViewController dealloc free all objects other than UI

In my custom subclass of UIView I:

  • create all subtasks in init and release it in dealloc
  • in layoutSubviews I set the subviews frames in such a way as to ensure that the frame is set only when changing. This is because redrawing subzones is expensive:
if ( !CGRectEqualToRect(__subview.frame, rect) ) { __subview.frame = rect; } 

This is what I do in all UIViewControllers. I do not use IB, everything is created programmatically.

Hope this helps!

+5
source

If you are using xib, where can I add / configure controls at the start at runtime?

In viewDidLoad controller.

If you are programming programmatically, should I do this in a ViewController (loadView?) Or a separate subclass of UIView? If the latter, as I indicate his file hosting, so that if it is added as a subtask, knows who his controller is?

Again, I always do this in the viewDidLoad my view controller.

What are you doing in awakeFromNib, loadView, viewDidLoad, UIView.init, UIViewController.init, etc.

Of these, I only viewDidLoad about viewDidLoad .

What other methods do you often use?

  • Make sure that you use auto-resize masks for your manually created controls to make sure they handle the user interface orientation correctly.

  • Never assume (as many do) that your screen is 320 pixels wide. Always refer to self.view.size where you need the size of the current view.

  • If your controls cannot handle user interface orientation changes using masks to automatically resize, be sure to configure them on iOS 5 viewWillLayoutSubviews .

+4
source

There are very simple tips, such as typical MainWindow.nib, that do not have localized content and can be played with a single line of code. In such cases, the code is much faster than unlocking nib.

There are also highly dynamic layouts that cannot be described as a tip.

You must choose the most convenient and effective method in each case. There will always be compromises, just choose wisely.

0
source

Source: https://habr.com/ru/post/917003/


All Articles