UIView subclass with delegate using NIB

I am trying to subclass UIView with nib. Using the following code:

- (void)awakeFromNib { [super awakeFromNib]; NSArray *v = [[NSBundle mainBundle] loadNibNamed:@"Qus_Scale1to7View" owner:self options:nil]; [self addSubview:[v objectAtIndex:0]]; } - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { NSArray *v = [[NSBundle mainBundle] loadNibNamed:@"Qus_Scale1to7View" owner:self options:nil]; [self addSubview:[v objectAtIndex:0]]; } return self; } 

this creates the object correctly, and the view is also displayed, and when the object is loaded from its nib, the delegate instantly becomes null and ignores any attempt to assign values โ€‹โ€‹to it.

Can anyone find out the reason why?

Thanks in advance.

+2
source share
2 answers

It will not reuse a single xib for multiple controllers. If you want to reuse this view, create a class that inherits from UIView and adds the code there.

 #import "SomeProtocol.h" @interface MyCustomView : UIView { IBOutlet UIView *headerView; IBOutlet UIView *footerView; IBOutlet UIButton *updateBtn; } @property (nonatomic, assign) id<SomeProtocol> delegate; @end ............ @implementation BCFirmwareView @synthesize delegate = _delegate; + (id)viewFromNibWithName: (NSString*)name { UIView *view = nil; NSArray *views = [[NSBundle mainBundle] loadNibNamed: name owner: self options: nil]; if (views) { for (UIView *aView in views) { if ([aView isKindOfClass: NSClassFromString(name)]) view = aView; } } return view; } - (id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder: aDecoder]; if (self) { } return self; } - (id)init { self = [[MyCustomView viewFromNibWithName: @"MyCustomView"] retain]; if (self) { } return self; } - (void)dealloc { self.delegate = nil; [headerView release]; [footerView release]; [updateBtn release]; [super dealloc]; } - (void)awakeFromNib { [super awakeFromNib]; // Do any additional setup after loading the view from its nib. headerView.backgroundColor = [UIColor redColor]; footerView.backgroundColor = [UIColor greenColor]; } - (void)willMoveToSuperview:(UIView *)newSuperview { [super willMoveToSuperview: newSuperview]; if (!newSuperview) return; } - (void)didMoveToSuperview { [super didMoveToSuperview]; } - (IBAction)updateBtnPressed: (id)sender { // do some stuff } @end 

The next step is to open xib in Interface Builder and set your class as a custom class for presentation, not for File Responder. Right-click on the view and make connections to the outlet and actions.

change the custom class for the view

make the outlet and actions connections for the view

Now you can simply instantiate your MyCustomView in any view controller and use it. It will work from the Builder interface if you remember to change your custom view class in your class.

+3
source

You can create a custom UIView with Xib and add properties to it. Then you bind the class to xib and bind the properties to IB.

Or you can use only

 NSArray *v = [[NSBundle mainBundle] loadNibNamed:@"Qus_Scale1to7View" owner:self options:nil]; UIView *view = [v objectAtIndex:0]; 

and set the values โ€‹โ€‹of your objects using the viewWithTag: method.

 UILabel *label = (UILabel *)[view viewWithTag:yourTag]; 

Let me know if this helps.

0
source

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


All Articles