0

I use the Paging-Functionality of the UIScrollView for paging several Views (an array of Views).

ViewController - View - ScrollView

My main UIViewController contains a View, inside the View the UIScrollView, which is connected via outlet to my ViewController.h. I created an xib-File to add a custom View (MyView) to the project; same with the class; i add a subclass of UIView, named MyView. The outlets from MyView.xib I connected to the MyView.h. And changed the Class in MyView.xib from UIView to MyView.

To interact with the MyView.xib out of the ViewController (UIScrollView...), i added a property to ViewController @property (nonatomic, weak) MyView *myView; So i'm able to set text, background-color and something else of MyView.

The whole stuff works as it should, but i'm not sure - is it bad-style? So I ask you guys; is that ok what I do, or isn't it?

Thomas
  • 330
  • 1
  • 11

1 Answers1

1

It's actually excellent style.

My main UIViewController contains a View, inside the View the UIScrollView, which is connected via outlet to my ViewController.h. I created an xib-File to add a custom View (MyView) to the project; same with the class; i add a subclass of UIView, named MyView. The outlets from MyView.xib I connected to the MyView.h. And changed the Class in MyView.xib from UIView to MyView.

That's the point of the class label in IB. Elements were meant to be subclassed, and in doing so, allow for deeper customization than the standard UIKit class has built-in.

To interact with the MyView.xib out of the ViewController (UIScrollView...), i added a property to ViewController @property (nonatomic, weak) MyView *myView; So i'm able to set text, background-color and something else of MyView

Again, a brilliant, but often overlooked aspect. Interface builder outlets were always meant to be weak (or assign), because they are usually maintained by strong top-level objects (say, superviews, or classes). The upside to making it weak is that when your class is deallocated, so are the weak outlets, automatically! See here for some better explanations of why IBOutlets should be weak.

Community
  • 1
  • 1
CodaFi
  • 43,043
  • 8
  • 107
  • 153
  • Thanks for your reply! And the View hirachy? ViewController - View - ScrollView is it possible without the View? ViewController - ScrollView In other words, is it possible to set the ScrollView to the ViewControllers View? – Thomas Jun 28 '12 at 09:18
  • 1
    Most people will say no, but I'm not most people! Because UIScrollView inherits from UIView, it is possible to assign it to self.view in `-loadView`. Allocate a fresh instance there, and it'll work great. Though this goes against what a UIViewController is for... Oh well. – CodaFi Jun 28 '12 at 09:19