简体   繁体   English

与控制器分开的视图

[英]separate view from controller

In order to respect the MVC pattern, I would like to dissociate the view from the controller. 为了遵守MVC模式,我想将视图与控制器分离。 For example 例如

  • QuestionView (inherits from UIVIew) QuestionView(从UIView的继承)
  • QuestionViewController (inherits from UIViewController QuestionViewController(继承自UIViewController

In my controller, I set the view self.view = ... 在我的控制器中,我设置视图self.view = ...

But when I push a button in the view, it should call a method from the controller BUT the view shouldn't know its controller right ? 但是,当我在视图中按下一个按钮,它应该调用来自控制器的方法,但认为不应该知道它的控制权?

So how can I link the view to the controller ? 所以,我怎么可以链接视图控制器?

以编程方式将按钮目标设置为QuestionViewController的对象。

With iOS in most cases you can get the job done with only sub classing the controller part, and not the views. 在大多数情况下,使用iOS,您可以仅对控制器部件进行子分类,而不能对视图进行分类。 So you use the UIKit provided classes straight 'out of the box'. 因此,您可以直接使用UIKit提供的类。

It's is possible because: 之所以可能是因为:

  • The layout: this can be stored in a Nib file and be loaded by the controller. 布局:可以存储在Nib文件中,并由控制器加载。
  • responding to user events: the UIcontrols have generic callback mechanisms: delegates and actions. 响应用户事件:UI控件具有通用的回调机制:委托和动作。 The 'connection' is either made in the nib file, or in the controller code. “连接”是在nib文件中或在控制器代码中进行的。

Personally, I only subclass views when I need custom drawing. 就个人而言,我仅在需要自定义绘图时才将视图分类。

So, the View INSTANCE obviously 'knows' its controller, but it is all done through generic interfaces, and so the view CODE is ignorant of your controller. 因此,View INSTANCE显然“知道”其控制器,但这都是通过通用接口完成的,因此View CODE不了解您的控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将字符串从IBAction传递到单独的View Controller - Passing a string from IBAction to a separate View Controller 如何在不使用界面构建器的情况下将视图与控 - How to separate view from controller without using interface builder 加速度计如何在单独视图的控制器中使用? - How can the accelerometer be used in a controller with a separate view? 如何使用单独的View Controller中的按钮更改WebView - how to change WebView with buttons in separate View Controller 从两个单独的 Web 服务获得响应后如何关闭视图控制器 - How to dismiss a view controller after getting response from two separate web services OBJ-C:从单独的视图控制器调用核心图图形的实例 - OBJ-C: Call an instance of core-plot graph from separate view controller UIBarButtonItem具有单独的纵向和横向图像 - 从UINavigationController弹出视图控制器时未调用layoutSubviews - UIBarButtonItem with separate portrait and landscape images - layoutSubviews not called when popping a view controller from UINavigationController 从子视图控制器推送视图控制器 - Pushing View Controller from Child View Controller 从内部视图控制器推送视图控制器 - Pushing view controller from inner view controller 与另一个视图控制器中的视图控制器进行交互 - Interact with a view controller from another view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM