'Casting' 一个在 Storyboard 中定义的 UIView 的子类
'Casting' a UIView defined in Storyboard to a subclass
这可能是一个非常糟糕的问题,所以我提前道歉。如果有更好的方法来解决这个问题,请随时告诉我。
我正在使用故事板来布置 objects 的初始安排。假设我在情节提要板上放了一个 UIView
,然后我 link 将视图 属性 放在我的 ViewController.m
文件中,名为 storyboardView
。在运行时,UIView
可能会发生很多事情,并且遵循 MVC 模式,我希望管理该行为的代码存在于单独的子类中。我怎样才能 'cast' 那 UIView
以便它现在响应子类而不是 ViewController?
我正在考虑类似的方法,但这行不通。它没有抛出任何错误,但背景颜色没有变红,所以我知道我不成功:
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *storyboardView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.storyboardView = [[MyView alloc] initWithFrame:self.storyboardView.frame];
}
@end
子类 Header:
#import <UIKit/UIKit.h>
@interface MyView : UIView
@end
子类实现:
import "MyView.h"
@implementation MyView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor redColor];
}
return self;
}
@end
在情节提要中,您可以像这样将 UIView
更改为任何子类
这可能是一个非常糟糕的问题,所以我提前道歉。如果有更好的方法来解决这个问题,请随时告诉我。
我正在使用故事板来布置 objects 的初始安排。假设我在情节提要板上放了一个 UIView
,然后我 link 将视图 属性 放在我的 ViewController.m
文件中,名为 storyboardView
。在运行时,UIView
可能会发生很多事情,并且遵循 MVC 模式,我希望管理该行为的代码存在于单独的子类中。我怎样才能 'cast' 那 UIView
以便它现在响应子类而不是 ViewController?
我正在考虑类似的方法,但这行不通。它没有抛出任何错误,但背景颜色没有变红,所以我知道我不成功:
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *storyboardView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.storyboardView = [[MyView alloc] initWithFrame:self.storyboardView.frame];
}
@end
子类 Header:
#import <UIKit/UIKit.h>
@interface MyView : UIView
@end
子类实现:
import "MyView.h"
@implementation MyView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor redColor];
}
return self;
}
@end
在情节提要中,您可以像这样将 UIView
更改为任何子类