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
}