【发布时间】:2012-07-07 14:17:40
【问题描述】:
我是 iOS 开发的新手,我正在从事一个使用 iOS5 和 Storyboard 的项目。我想创建一个可重用的组件,比如一个按钮,带有一个自定义视图(在按钮内部会有一些图像和标签,它将是一个大按钮),在故事板的同一个视图中创建其中一些。 所以我创建了一个带有 UIButton 的 XIB 文件 (ItemClothes.xib),其中包含一些 UIImagesViews 和一个 UILabel。然后我创建了 UIButton 的子类(UIItemClothes.h 和 UIItemClothes.m),具有 UIImageViews 和 UILabel 组件的属性。
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
然后在 .XIB 文件中,我将 UIButton 的类设置为 UIItemClothes,并在 XIB 的 UI 组件和我的类属性之间建立关系。
因此,在那之后,在情节提要中,在一个视图的 ViewController 中,我编写了以下代码:
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];
如您所见,该组件将位于 TableViewCell 内,其想法是在其中放置更多组件(UIItemClothes)。
问题是组件已绘制,但任何插座都设置为我在上面的代码中所做的。
谁能帮帮我? 谢谢!
【问题讨论】:
-
好吧,问题已经解决了……不再需要 UIButton 的自定义子类,而是需要一个带有所有 Outlets 的 ViewController。然后在另一个 ViewController(StoryBoard)中初始化这个新的 ViewController。
-
您应该发布您的答案并接受它作为正确的答案,以便其他人也可以从中受益。
标签: ios uibutton storyboard xib reusability