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

I have been fighting for this for several hours: I want to create a custom UIView that is loaded from a NIB file. this is what i did:

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

2) I placed the class as my own file owner class.

3) To download the NIB file, I know that 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) to use this custom view. Using IB, I created a UIView in my main ViewController. in the properties of the view in the user class, I put "MyView". I created an IBOutlet for this view called "myView" and connected it to the ViewController class.

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

what am I doing wrong? is there another way to do this?

thank you Nimrod


EDIT: I changed my code according to the answers here. all the answers here did not actually use the user class, which is the whole idea here. this is what i did, but the application crashes because it thinks testView is a UIView, not 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 } 
+4
source share
3 answers

try it

 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; } 
+4
source

To use the nib file in my application, I advise you to do it this way easier

-1) create a Nib file with all the necessary components, say, for example, it contains UILabel and UIButton.

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

-3) in your code, wherever you want, either in init or from Methode to viewController, here is what 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 user view, you can change the text of the label in this way: myLabel.text=@ "someText";

You can add an action to your button:

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

Here is what I did to implement using a custom class file. I tried to add a toolbar to the keyboard, and when I created separate XIB files connected to the .h and .m files ... so in the .m file, I used your code in the initWithFrame method as follows:

  - (id)initWithFrame:(CGRect)frame { NSArray * nib = [[NSBundle mainBundle] loadNibNamed: @"MyView" owner: self options: nil]; self = [nib objectAtIndex:0]; return self; } 
0
source

All Articles