views:

155

answers:

2

Hello -

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

+1  A: 

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:

[theMap addSubView:dataTag.view];

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's initWithNibName:bundle method. Otherwise, you'll have to implement the loadView method 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:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
    {
    }
} 

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.

Typeoneerror
thank you for the assistance - please see my edits above
Brodie
- (id)initWithNibNamed:(NSString *)DataTagViewController bundle:bundle .... you don't want your variable name to be the same as a class name.
Typeoneerror
great - i got the object created and there is no crash, but the datatag is still not visible in the view.
Brodie
A: 

What is the parent class of DataTagViewController? You say you need to create "my own UIView class", but your example suggests that you actually want to create UIViewController subclass. initWithNibNamed: is UIViewController method. If your parent is UIView, then "unrecognized selector" makes sense.

Jaanus
perhaps i am doing it all wrong - All I really want is to be able to create a view (called datatag) that I have created that contains 5 uilabels that I need to be able to constantly modify the data in. I created a UIViewController Class called DataTagViewController, then designed the DataTagViewController.xib.
Brodie