How to create the controller and the UIView progra

2019-09-01 06:29发布

I am trying to learn some of the basics about creating views and view controllers on iOS. All the samples and documentation I've come across use the Interface Builder and NIB files to create UIViewControllers and UIViews. How to create the controller and the UIView programmatically and not use the interface builder?

3条回答
成全新的幸福
2楼-- · 2019-09-01 06:43

The short answer is that yes, of course you can create views and view controllers programmatically.

As someone who had this same feeling when I first started out, let me impart this short bit of wisdom: Do not try and remove all .xib files from your project until you know a lot more about what you are doing!

There are tutorials out there on how to remove the .xib files entirely from your project, and remove dependency on MainWindow.xib. In my experience it is definitely not worth your time. Just leave the .xib files in your resources folder, close it up and pretend they don't exist.

Eventually, you may even be happy they are still there.

查看更多
孤傲高冷的网名
3楼-- · 2019-09-01 06:54

You may do that, that's for sure. But you should see the pros and cons here;

You create and manage everything in code, neat huh?

  • Well this means, you will retain/release everything yourself.
  • You will write lots of boilerplate code just to create a complex view with more than one layer of component hierarchy.
  • You will not see the properties you may change, instead you need to see the Class Reference document for each component.
  • You need to play with pixel values a lot, i cannot emphasize how long this "a lot" will eventually be.

So, consider Interface Builder, it is easier to keep everything seperate, and then bind them as needed. Code maintenance is much more important in the later phases, as the app becomes mature.

I have been through both ways, and my vote is +1 for Interface Builder, and override stuff only when needed. That is in practice less than like 5% of your UI development time.

查看更多
4楼-- · 2019-09-01 06:56

Yes,

You can create UIViewController and UIView programmatically,

See the Apple documentation for UIViewController and UIView, there are many function which start with init, used to create programmatically.

Suggest you to invest some time reading Documentation.

查看更多
登录 后发表回答