I need to create my own UIView class and it is not something I have had to do. I created the class, then laid out the small view in IB (it’s just a few labels that i will later need to add data to ). but now im stuck on how to actually put an instance of it in my main view. Can someone point me in the direction of a good tutorial? The closest thing I have done to this is creating a custom tableViewCell.
DataTagViewController.m:
- (id)initWithNibNamed:(NSString *)DataTagViewController bundle:bundle {
if ((self = [super initWithNibName:DataTagViewController bundle: bundle])) {
// Custom initialization
}
return self;
}
MapView.m:
DataTagViewController *dataTag = [[DataTagViewController alloc] initWithNibNamed:@"DataTagViewController" bundle:nil];
[theMap addSubView: dataTag.view]; <<< this line causes the crash (theMap is a UIView)
I now get this runtime error when adding the subview:-[UIView addSubView:]: unrecognized selector sent to instance 0x470f070′
2010-06-06 21:22:08.931
UIViewController is not a view, but controls a view. If your DataTagViewController class extends UIViewController, then you’ll want to add it’s view, not the class itself:
Also, do you have a DataTagViewController.xib file created that has your view in it? If you don’t, you’ll need to create one and use the
UIViewController‘sinitWithNibName:bundlemethod. Otherwise, you’ll have to implement theloadViewmethod instead to provide your own view via code.Edit
Your init function is using the name of your class as a variable. That probably won’t work. Use the default sigature:
If you aren’t doing anything beyond the init function, you don’t need to implement this method. Your alloc/init statement is enough.
For a good tutorial, read the View Controller Programming guide in the docs.