[英]Using a subclass of UIViewController subclass with subclass of UIView subclass
I have an MVC architecture of a certain view within my app.我的应用程序中有一个特定视图的 MVC 架构。 Now I want to create a subclass of the View and Controller part.
现在我想创建视图和控制器部分的子类。 The original
UIViewController
subclass is loaded with a UIView
subclass from storyboard.原始
UIViewController
子类加载了来自故事板的UIView
子类。 How can I make my subclass of this UIViewController
subclass use my subclass of the UIView subclass code when it loads its view?我怎样才能让这个
UIViewController
子类的子类在加载视图时使用我的 UIView 子类代码的子类?
EDIT编辑
Here's some more details about what I want to do.这里有一些关于我想做的事情的更多细节。 I currently have these classes:
我目前有这些课程:
ControlView
ControlViewController
which are connected with storyboard.与故事板相关联。 I load an instance of ControlViewController using:
我使用以下方法加载 ControlViewController 的实例:
self.storyboard!.instantiateViewControllerWithIdentifier("ControlViewController")! as! ControlViewController
Now what I want to do is subclass ControlView
so that I can change some constraints and add a few extra subviews.现在我想要做的是子类
ControlView
以便我可以更改一些约束并添加一些额外的子视图。 Let's call this subclass ExpandedControlView
.我们称这个子类为
ExpandedControlView
。 I also want to subclass ControlViewController
because I want to add some extra actions from ExpandedControlView
to the controller.我还想
ControlViewController
因为我想从ExpandedControlView
添加一些额外的操作到控制器。 This can be called ExpandedControlViewController
.这可以称为
ExpandedControlViewController
。
Then I want to be able to instantiate ExpandedControlViewController
and use ExpandedControlView
like ControlViewController
would with ControlView
.然后我希望能够实例化
ExpandedControlViewController
并像ControlViewController
一样使用ExpandedControlView
和ControlView
。
In storyboard, select the hierarchy view.在故事板中,选择层次结构视图。
Then select your UIViewController's View in the hierarchy on the left, and select that view's class in the identity inspector on the right.然后在左侧的层次结构中选择您的 UIViewController 的视图,并在右侧的身份检查器中选择该视图的类。
Or if you want to do it in code.或者如果你想在代码中做到这一点。 Override viewDidLoad on ControlViewController, instantiate your custom view and set your ControlViewController's view property to your custom view.
覆盖 ControlViewController 上的 viewDidLoad,实例化您的自定义视图并将您的 ControlViewController 的视图属性设置为您的自定义视图。
-(void)loadView{
ControlView *myCustomView = [[ControlView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame];
self.view = myCustomView;
}
One caveat when doing this is that the layout of any subviews might change from the time loadView is called.这样做时的一个警告是,任何子视图的布局可能会从调用 loadView 时发生变化。 So if you have subviews in your custom view, you should also override layoutSubviews in your custom view and set all your view's subviews' frame property again there.
因此,如果您的自定义视图中有子视图,您还应该覆盖自定义视图中的 layoutSubviews 并在那里再次设置所有视图的子视图的框架属性。
在视图控制器子类中实现 loadView 并使用自定义视图子类的实例设置视图属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.