How to create a Custom UIView and How to instantiate that ?

Discussion in 'iOS Programming' started by Bracer, Dec 25, 2009.

  1. Bracer macrumors newbie

    Joined:
    Nov 24, 2009
    #1
    How to create a Custom UIView and How to instantiate that ?

    In Flash, all we need to do is create a MovieClip and assign it a class name in the library.

    Instantiation is a simple matter of:
    Code:
    var newclass:CustomClass = new CustomClass({initialization_data:1652});
    addChild(newclass);
    
    At this current point in time, my understanding of creating custom UIView in xcode is limited to the understanding that you have to:
    1: Forward declare the class in the header.
    2:
    Code:
    newclass *CustomClass
    in @interface's {}
    and THEN
    Code:
    @property (nonatomic, retain) newclass *CustomClass;
    in the header file also.
    3: Nib initialization in the implementation file in viewDidLoad and do "addSubview".

    ==============================
    Based on my limited understanding at this point, I find the need to
    Code:
    @property (nonatomic, retain) newclass *CustomClass;
    
    at the header somewhat limiting...as it denotes you must know how many instance you will create in the application before hand.

    I believe I am wrong here, hence, my question is, how do I add custom UIViews with initialization codes in real time ?
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    The location of where the view is declared depends how you're going to use it.

    Assuming you're going to be using it later on after you've created it, you'll do pretty much what you described:
    Code:
    // .h
    @interface MyController : MyObject {
        UIView *myView;
    }
    @end
    
    // .m
    @implementation MyController
    - (void)someMethod {
        myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        [aSuperView addSubview:myView];
    }
    
    - (void)dealloc {
        [myView release];
        [super dealloc];
    }
    @end
    If you will have multiple views, instead declare an NSMutableArray in your class, and declare the view locally in your method and add the views to your array after adding them to your superview.

    A @property is only required if you are going to be accessing the object outside of the class.

    It sounds like you don't really know Objective-C. I suggest reading a book on it.
     

Share This Page