Doug Smith Doug Smith - 6 months ago 19
iOS Question

Is it not possible to create a UIViewController programmatically without building the view?

I want to create a UIViewController programmatically. No nibs or storyboards. It will basically be created and then presented, and in its own class it will set up an image to be displayed.

However, whenever I just

alloc init
a view controller and present it, it comes out black as it is presented.

I assume this is because it lacks its own view?

When I create a raw
UIViewController
in Storyboard it automatically sets it up with a view, how do I set the view up so like the Storyboard it's the proper device size, changes on rotation, etc.?

Answer

However, whenever I just alloc init a view controller and present it, it comes out black as it is presented.

Because it is just a raw UIViewController. You have no way to modify / customize it; it isn't your class.

Instead, create a UIViewController subclass, e.g. MyCoolViewController, and instantiate and present that. The difference is that with MyCoolViewController you can override viewDidLoad and other methods so as to put some useful interface into its view.

how do I set the view up so like the Storyboard it's the proper device size, changes on rotation, etc

Don't worry; all that will happen automatically, because the view is managed by a view controller and the view controller is placed correctly into the view controller hierarchy (e.g. by saying presentViewController:...).