【发布时间】:2015-04-01 00:32:03
【问题描述】:
这可能是一个非常糟糕的问题,所以我提前道歉。随时告诉我是否有更好的方法来解决这个问题。
我正在使用情节提要来布置对象的初始排列。假设我在情节提要上放置了UIView,并将该视图链接到我的ViewController.m 文件中名为storyboardView 的属性。在运行时,UIView 可能会发生很多事情,并且遵循 MVC 模式,我希望控制该行为的代码存在于单独的子类中。如何“转换”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
子类头:
#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
【问题讨论】:
标签: ios objective-c model-view-controller uiview