Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Bracer

macrumors newbie
Original poster
Nov 24, 2009
14
0
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 ?
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.