IOS5 Assign .xib to UIView Custom Class

HI has a custom view class that loads and fits into my main view using the following code. The reason is because I want to fill it with different content, and not create a view in the code every time, if I create a custom class, I can reuse it in a loop, etc., I got this to work very well in the code , i.e. put stickers on buttons, etc.

But instead of drawing code, I thought that if I create a new kind of user interface, then I will visually create my own text fields, labels and buttons on this view.

Then connect it to my custom class.

Bu is where I had a problem, how to connect this kind of xib view file so that it becomes visible when it was placed in my code. I assigned a custom class attribute in the xib file to my custom file, but what else am I missing?

.h file:

#import <UIKit/UIKit.h> @interface blogView : UIView @end 

.m file:

  #import "blogView.h" @implementation blogView - (id)init { self = [super initWithFrame:CGRectMake(0, 0, 478, 220)]; if (self != nil) { NSLog(@"Blog View loaded"); self.backgroundColor = [UIColor yellowColor]; UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 400, 40)]; [titleLbl setText:@"This is the Title"]; [self addSubview:titleLbl]; } return self; } @end 

my xib file has the same blogView.xib name as the user interface.

In my main view controller and in ViewDidLoad I have

  blogView *blogItem = [[blogView alloc]init]; [self.view addSubview:blogItem]; 

When I run this, everything works fine, but I would like to link the .xib file to save time, etc. Thanks

+6
source share
3 answers

Having looked around well and tried pieces of tips and tricks, I managed to do this with the following:

In my .m file I put / Modified the following:

  self = [super init]; if (self != nil) { NSArray *theView = [[NSBundle mainBundle] loadNibNamed:@"blogView" owner:self options:nil]; UIView *nv = [theView objectAtIndex:0]; .. rest of code. [self addSubview:nv]; .. rest of code. 

Many thanks

+14
source

I struggled with this for an hour when I CHANGED my viewcontroller class. This is what worked for me in Xcode 5

  • Go to your XIB file.
  • Click the transparent file owner box on the left.
  • Open the checks tab (the third button on the right in the "View" section is between the editor and the organizer)
  • Go to your identity inspector (third from the left) under the view tab of the editor organizer.
  • Correct the custom class - Class parameter for any class that you want to respond to.

Let's just say that I was very annoyed by spending time on the fact that

+4
source

You might want to create a controller for your view and load this view with initWithNibName: bundle:

0
source

Source: https://habr.com/ru/post/907442/


All Articles