【问题标题】:'Casting' a UIView defined in Storyboard to a subclass将 Storyboard 中定义的 UIView '铸造'到子类
【发布时间】: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


    【解决方案1】:

    在情节提要中,您可以将UIView 更改为像这样的任何子类

    【讨论】:

      猜你喜欢
      • 2012-03-08
      • 2014-08-09
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多