Search code examples
iosuibuttonstoryboardxibreusability

iOS 5 create custom UIButton reusable component with .XIB


I'm newbie in the iOS development and I'm working in a projecte that it uses iOS5 and Storyboarding. I would like to create a reusable component, like a button, with a custom view (inside the button there will be some images and labels, it will be a big button), to create some of them in the same view of a Storyboard. So I've created a XIB file (ItemClothes.xib) with a UIButton and inside of it some UIImagesViews and a UILabel. Then I've created a subclass of UIButton (UIItemClothes.h and UIItemClothes.m), with properties for UIImageViews and UILabel components.

UIItemClothes.h

#import <UIKit/UIKit.h>
#import "SCClothes.h"

@interface UIItemClothes : UIButton

@property (nonatomic, strong) IBOutlet UIImageView *imageClothes;
@property (nonatomic, strong) IBOutlet UIActivityIndicatorView *loadingImage;
@property (nonatomic, strong) IBOutlet UIImageView *seasonIconClothes;
@property (nonatomic, strong) IBOutlet UIImageView *categoryIconClothes;
@property (nonatomic, strong) NSString *idClothes;
@property (strong, nonatomic) IBOutlet UILabel *lblProva;

@end

UIItemClothes.m

#import "UIItemClothes.h"

@implementation UIItemClothes

@synthesize imageClothes = _imageClothes;
@synthesize loadingImage = _loadingImage;
@synthesize seasonIconClothes = _seasonIconClothes;
@synthesize categoryIconClothes = _categoryIconClothes;
@synthesize idClothes = _idClothes;
@synthesize lblProva = _lblProva;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self addSubview:[[[NSBundle mainBundle] loadNibNamed:@"ItemClothes" owner:self options:nil] objectAtIndex:0]];
    }
    return self;
}

@end

Then in the .XIB file I've set the class of UIButton as UIItemClothes, and make the relationships between XIB's UI components and my class properties.

So, after that, in the Storyboard, in the ViewController of one view I've written this code:

UIItemClothes *item = [[UIItemClothes alloc] initWithFrame:CGRectMake(0.0, 0.0, 200.0, 200.0)];
item.lblProva.text = @"test!";
[item.categoryIconClothes setImage:iconCategory];
item.seasonIconClothes.image = iconSeason;
[cell addSubview:item];

As you see, this component will be inside a TableViewCell, and the idea is to put more components (UIItemClothes) inside of it.

The problem is that the component is drawed but any outlet is set as I do in the code above.

Can anyone help me? Thanks!


Solution

  • well, the problem has been resolved... Instead of having a custom subclass of UIButton, there will be needed a ViewController with all Outlets. Then in the other ViewController (StoryBoard) is initialized this new ViewController