Nimrod Yizhar
Nimrod Yizhar

Reputation: 381

creating a custom UIVIew with a NIB file without a view controller

I'm struggling for a few hours about this: I want to create a custom UIView which is loaded from a NIB file. this is what I did:

1)I created a class and a NIB file named "MyView".

2)I put the class as the file's owner custom class.

3)In order to load the NIB file, I know I need this code, but I'm not sure where to put it. I put it in the "init" method.

    NSArray * nib = [[NSBundle mainBundle]
                     loadNibNamed: @"MyView"
                     owner: self
                     options: nil];

    self = [nib objectAtIndex:0];

4)for using this custom View: Using IB, I created a UIView in my main ViewController. in the view properties in custom Class I put "MyView". I created an IBOutlet for this view called "myView" and connected it to ViewController class.

5) now I'm not sure if I need to call "init" or if it's done automatically because it's in the NIB file. I tried both of them. anyway, "myView" class type is recognized as a UIView and not as a MyView, and is displaying an empty view.

what am I doing wrong? it there another way to do it?

thanks, Nimrod


EDIT: I changed my code according to the answers here. all of the answers here didn't actually use a custom class, which is the whole idea here. here's what I did, but the app crashes because it thinks that testView is a UIView and not a TestView. please help.

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic,retain) TestView *testView;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray* views=[[NSBundle mainBundle] loadNibNamed:@"TestView" owner:nil options:nil];
    self.testView= [views objectAtIndex:0];
    [self.view addSubview:self.testView]; 
    [self.testView trace]; // calling a method from the custom class
}

Upvotes: 4

Views: 4384

Answers (3)

rckehoe
rckehoe

Reputation: 1198

Here is what I did to implement using a custom class file. I was attempting to append a toolbar to a keyboard and when I created seperate XIB files that connected to the .h and .m files... so in the .m file, I used your code within the "initWithFrame" method like so:

 - (id)initWithFrame:(CGRect)frame
 {

      NSArray * nib = [[NSBundle mainBundle]
                 loadNibNamed: @"MyView"
                 owner: self
                 options: nil];

      self = [nib objectAtIndex:0];

      return self;

 }

Upvotes: 0

Rifinio
Rifinio

Reputation: 943

To use a nib file in your app i advise you to do it this way, it's easier

-1) create the Nib file with all the components you need, let's say for instance it contains a UILabel and a UIButton.

-2) you give each component a TAG number (ex: 100,101)

-3) in your code , where ever you want, either in the init or from a Methode in a viewController here is how you call it:

NSArray* views=[[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:nil options:nil];
    // PS: change "MyCustomView" to your nib file name "MyView" for instance
    // and do not add the .xib extension 
UIView* myCustomView= [views objectAtIndex:0];
// always parse the retrieved components so that you could use them in a proper way
UILabel*    myLabel=(UILabel*)      [myCustomView viewWithTag:100];
UIButton*   myButton= (UIButton*)      [myCustomView viewWithTag:101];
//if you are calling this from a UIView Class
 [self addSubview:mainView]; // add the customview to the main view
//if you are calling this from a ViewController uncomment this line below
//[self.view addSubview:mainView];

Done , now you can interact with your custom view, you can change the label text this way: myLabel.text=@"someText";

you can add an action to your button :

[myButton addTarget:self action:@selector(someAction) forControlEvents:UIControlEventTouchUpInside];

Upvotes: 1

iAppDeveloper
iAppDeveloper

Reputation: 1088

Try this

if ((self = [super initWithCoder:aDecoder])) {
        if ((self = [super initWithCoder:aDecoder])) {
           NSArray * nib = [[NSBundle mainBundle]
                     loadNibNamed: @"MyView"
                     owner: self
                     options: nil];
          [self addSubview:nib[0]];
        }
        return self;
    }

Upvotes: 4

Related Questions