nonopolarity
nonopolarity

Reputation: 151036

For iOS, what does changing ViewController.xib custom class to another class mean?

Using XCode 4.3.2, if a new class TestView is added (with TestView.h and .m added), and it is a subclass of UIView, and it is drawing things inside the drawRect method. Why does the file ViewController.xib has be clicked on, and the Identity Inspector has to be clicked on, and the Custom Class has to be changed from UIView to TestView?

What does doing so mean? What if that is not changed -- can the code in drawRect in TestView.m still be easily invoked? Alternatively, can all the code in TestView.m be moved to ViewController.m? If so, how can that be done, as TestView inherits from UIView, but ViewController inherits from UIViewController and ViewController's drawRect may not get called.

Upvotes: 0

Views: 210

Answers (1)

danh
danh

Reputation: 62676

The common way to do custom drawing is to implement a UIView subclass and implement drawRect. The only way to make that drawing visible is to add the custom view to a view hierarchy which is controlled by - but not drawn by - a view controller.

The xib describes the user interface. Adding a UIView subclass (like TestView) in the xib causes an instance of that class to be allocated an attached to the view hierarchy. The class name is required to know which class to instantiate (make an instance of).

drawRect is a UIView method that is called by the system when it decides that some or all of the view needs to be drawn. Applications do not call it directly. They implement it and let the system call it.

ViewControllers don't draw anything, they only manage the hierarchy of views. So there's no point in implementing drawRect in a view controller.

Upvotes: 1

Related Questions