使用故事板在 UIViewController 中初始化自定义 UIView [英] Initialization of a custom UIView in a UIViewController using a storyboard
问题描述
我有一个自定义 UIView 控制器,想初始化并分配一个我之前分配给 IBOutlet 的自定义 UIView.我正在使用故事板.谁能给我提示在哪里调用自定义 UIView 的指定初始值设定项?
I have a custom UIViewcontroller and wanted to initialize and assign a custom UIView which I assigned to an IBOutlet before. I'm using a storyboard. Can anybody give me hints where to call the designated initializer of the custom UIView?
**MyCustomUIView.h**
@interface MyCustomUIView : UIView
@end
**MyCustomUIView.m**
@implementation MyCustomUIView
- (id)initWithNumberOfHeaderRows:(NSUInteger)headerRowCount numberOfHeaderColumns:(NSUInteger)headerColumnCount frame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
@end
**MyUIViewController.h**
@interface MyUIViewController : UIViewController
@property (weak, nonatomic) IBOutlet MyCustomUIView *myCustomUIView; // I wanna use this Outlet
@end
**MyUIViewController.m**
@implementation MyUIViewController
@end
这是所用 github 源的抽象版本:https:///github.com/mutualmobile/MMSpreadsheetView/blob/master/MMSpreadsheetView/MMSpreadsheetView.m
This was an abstract version of the used github source: https://github.com/mutualmobile/MMSpreadsheetView/blob/master/MMSpreadsheetView/MMSpreadsheetView.m
推荐答案
可以在控制器的 viewDidLoad 中调用自定义视图的构造器(初始化器).然后,将其添加为控制器视图的子视图.
You can call the constructor (initializer) of the custom view in viewDidLoad of the controller. Then, add it as a subview of the view of the controller.
像这样:
- (void)viewDidLoad {
...
MyCustomUIView *customView = [MyCustomUIView alloc] initWithNumberOfHeaderRows:0 numberOfHeaderColumns:0 frame:CGRectZero];
[self.view addSubview:customView];
...
}
更新
我认为您应该像这样创建自定义视图类:
I think you should create your custom view class something like this:
//MyCustomView.h
@interface MyCustomView : UIView
- (id)initWithNumberOfHeaderRows:(NSUInteger)numberOfHeaderRows numberOfHeaderColumns:(NSUInteger)numberOfHeaderColumns;
@property (readwrite, nonatomic) NSUInteger numberOfHeaderRows;
@property (readwrite, nonatomic) NSUInteger numberOfHeaderColumns;
@end
//MyCustomView.m
@implementation
- (void)setup {
// Do custom stuffs here...
}
- (id)initWithNumberOfHeaderRows:(NSUInteger)numberOfHeaderRows numberOfHeaderColumns:(NSUInteger)numberOfHeaderColumns {
self = [self initWithFrame:CGRectZero];
if (self) {
self.numberOfHeaderRows = numberOfHeaderRows;
self.numberOfHeaderColumns = numberOfHeaderColumns;
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (id)init {
self = [super init];
if (self) {
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self setup];
}
return self;
}
@end
然后在 viewDidLoad:
Then in viewDidLoad:
- (void)viewDidLoad {
...
// Assuming you have an IBOutlet property of MyCustomView class with a name 'customView'
// That property must be hooked up from the xib/storyboard
self.customView.numberOfHeaderRows = 1;
self.customView.numberOfHeaderColumns = 1;
self.customView.frame = self.view.bounds;
...
}
更新 2
您可以简单地添加一个公共方法来设置自定义视图中标题行和列的数量.
You may simply add a public method for setting number of header rows and columns in the custom view.
//MyCustomView.h
@interface MyCustomView : UIView
...
- (void)setNumberOfHeaderRows:(NSUInteger)rows numberOfHeaderColumns:(NSUInteger)columns;
...
@end
//MyCustomView.m
@implementation MyCustomView
...
- (void)setNumberOfHeaderRows:(NSUInteger)rows numberOfHeaderColumns:(NSUInteger)columns {
//Do the custom stuffs that you want...
}
...
@end
然后在 viewDidLoad
Then in viewDidLoad
- (void)viewDidLoad {
...
[self.customView setNumberOfHeaderRows:10 numberOfHeaderColumns:4];
...
}
更新 3
根据您提供的参考文件,您可以在数据源中添加方法:
Based on the reference files that you've given, you can add methods in the DataSource:
//MMSpreadsheetView.h
@optional
...
- (NSUInteger)spreadsheetViewNumberOfHeaderRows:(MMSpreadsheetView *)spreadsheetView;
- (NSUInteger)spreadsheetViewNumberOfHeaderColumns:(MMSpreadsheetView *)spreadsheetView;
...
然后,在实现文件中:
//MMSpreadsheetView.m
...
- (void)setupWithNumberOfHeaderRows:(NSUInteger)rows numberOfHeaderColumns:(NSUInteger)columns {
_scrollIndicatorInsets = UIEdgeInsetsZero;
_showsVerticalScrollIndicator = YES;
_showsHorizontalScrollIndicator = YES;
_headerRowCount = headerRowCount;
_headerColumnCount = headerColumnCount;
if (headerColumnCount == 0 && headerRowCount == 0) {
_spreadsheetHeaderConfiguration = MMSpreadsheetHeaderConfigurationNone;
}
else if (headerColumnCount > 0 && headerRowCount == 0) {
_spreadsheetHeaderConfiguration = MMSpreadsheetHeaderConfigurationColumnOnly;
}
else if (headerColumnCount == 0 && headerRowCount > 0) {
_spreadsheetHeaderConfiguration = MMSpreadsheetHeaderConfigurationRowOnly;
}
else if (headerColumnCount > 0 && headerRowCount > 0) {
_spreadsheetHeaderConfiguration = MMSpreadsheetHeaderConfigurationBoth;
}
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.backgroundColor = [UIColor grayColor];
[self setupSubviews];
}
- (id)initWithNumberOfHeaderRows:(NSUInteger)headerRowCount numberOfHeaderColumns:(NSUInteger)headerColumnCount frame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setupWithNumberOfHeaderRows:rows numberOfHeaderColumns:columns];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
NSUInteger rows = 0;
NSUInteger columns = 0;
if (self.dataSource && [self.dataSource respondsToSelector:@selector(spreadsheetViewNumberOfHeaderRows:)]) {
rows = [self.dataSource spreadsheetViewNumberOfHeaderRows:self];
}
if (self.dataSource && [self.dataSource respondsToSelector:@selector(spreadsheetViewNumberOfHeaderColumns:)]) {
columns = [self.dataSource spreadsheetViewNumberOfHeaderColumns:self];
}
[self setupWithNumberOfHeaderRows:rows numberOfHeaderColumns:columns];
}
return self;
}
...
这篇关于使用故事板在 UIViewController 中初始化自定义 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!