iOS 布局;我不明白
好吧,“不明白”太严厉了;我已经让它工作了,这对我来说是一个合乎逻辑的设置,但它似乎不是 iOS 认为合乎逻辑的。所以我没有得到任何东西。
假设我有一个应用程序显示两条信息;一个日期和一个表格。根据 MVC 方法,我在这里使用了三个 MVC,一个用于日期,一个用于表格,另一个将这两个 MCV 放入屏幕中,并将它们连接起来。
主 MVC 知道如何/在何处布局两个子 MVC。每个细节 MVC 仅在主 MVC 指定的范围内处理其自己的子级。类似这样的:
- (void)loadView {
MVC* mvc1 = [[MVC1 alloc] initwithFrame:...]
[self.view addSubview:mvc1.view];
MVC* mvc2 = [[MVC2 alloc] initwithFrame:...]
[self.view addSubview:mvc2.view];
}
如果上面的内容是合乎逻辑的(这对我来说就是这样),那么我希望任何 MVC 类都有一个构造函数“initWithFrame”。但MVC没有,只有视图有这个。
为什么?
如何正确布局嵌套 MVC? (当然我不仅仅只有这两个,但是细节 MVC 又有子 MVC。)
感谢大家的回复。我将研究所提供的链接。
让我再次尝试解释一下我的问题,希望能更清楚地说明这一点。请注意,我已经发现我的视图与 iOS 的不匹配,因为我不喜欢我的代码的去向。
是的,我将 UIViewController 称为“MVC”,因为它目前对我来说实现了 MVC 的所有方面;它具有控制器代码和嵌入式视图,此外控制器通常还保存并提供数据(所有 TableView 示例都是这样实现的)。
MVC 可以存在于多个层面;基本上 UITextField 可以(应该?)是 MVC;有一个视图,但也涉及您不想与其他代码混合的控制器逻辑。封装。例如:Java的Swing JTextField有一个MVC。 JTable、JList...也是如此,多个 MVC 模式嵌套在其他 MVC 中以构建整个屏幕。
当某些平台说它使用 MVC 模式时,这正是我所期望的。因此,当我对表进行编码时,我创建了一个 MVC,并且只发送以日期作为参数的 loadData 消息。它需要自己处理其余的事情。我有一个可以滑入的Detail MVC;然后我告诉它需要显示的对象,并且它需要自己处理其余的事情。封装。
所以我有很多带有嵌入式 UIView 的 UIViewController。这不是这样做的方法......
Well, "not getting it" is too harsh; I've got it working in for what for me is a logical setup, but it does not seem to be what iOS deems logical. So I'm not getting something.
Suppose I've got an app that shows two pieces of information; a date and a table. According to the MVC approach I've got three MVC at work here, one for the date, one for the table and one that takes both these MCVs and makes it into a screen, wiring them up.
The master MVC knows how/where it wants to layout the two sub MVC's. Each detail MVC only takes care of its own childeren within the bounds that were specified by the master MVC. Something like:
- (void)loadView {
MVC* mvc1 = [[MVC1 alloc] initwithFrame:...]
[self.view addSubview:mvc1.view];
MVC* mvc2 = [[MVC2 alloc] initwithFrame:...]
[self.view addSubview:mvc2.view];
}
If the above is logical (which is it for me) then I would expect any MVC class to have a constructor "initWithFrame". But an MVC does not, only view have this.
Why?
How would one correctly layout nested MVCs? (Naturally I do not have just these two, but the detail MVCs have sub MVCs again.)
Thanks all for replying. I will study the links that were provided.
Let me try to explain my issue one more time, hopefully to making it more clear. Do note that I already figured out that my view does not match iOS's, since I do not like where my code is going.
Yes, I'm calling a UIViewController an "MVC", since it for me at the moment implements all aspects of a MVC; it has controller code and an embedded view, plus the controller usually also holds and provides the data (all TableView examples implement it like this).
MVC can be present on many levels; basically a UITextField could (should?) be a MVC; there is a view, but also controller logic involved that you do not want to mix with other code. Encapsulation. For example: Java's Swing JTextField has a MVC. So does a JTable, JList, ... Multiple MVC patterns nested in other MVC's to build a whole screen.
This what I expect when some platform says it uses the MVC pattern. So When I coded the table, I created a MVC and only send the loadData message with a date as the parameter to it. It needs to take care of the rest itself. I have a Detail MVC that can slide in; I then tell it the object it needs to show and it needs to take care of the rest itself. Encapsulation.
So I have a lot of UIViewControllers with embedded UIViews. And that is not the way to do it...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我理解您的问题(我可能不会,请参阅我对此的评论),我认为您应用 MVC 设计模式的粒度太细了。最常见的是,在您描述的设置中,您将拥有一个模型、一个控制器和多个分组/组合的视图,如
.xib
文件中一样。用 Cocoa Touch 术语来说,您将拥有一个 UIView,其中包含一个带有日期的 UILabel 和一个用于表格的 UITableView。这些是您的观点。
您肯定会有一个表数据的模型,可能是一个数据数组。如果您的日期数据是从某些东西检索或计算的日期或其他完全独立于数据数组的东西,那么您的日期数据可能来自其自己的模型。如果它与数组数据相关联——它们都是从数据库中提取的,或者日期是根据数组数据计算的,或者你有什么——那么你就有了一个模型。
如果数据全部来自单个模型,那么单个控制器可能就可以了。即使数据来自多个源/模型,您在此设置中可能只需要/想要一个控制器。 UITableView 将有一个 UITableViewController,并且该控制器也可以负责提供您的日期。
总而言之,模型视图控制器设计模式不需要拥有一堆嵌套的模型、视图和控制器集。它们可能是,并且足够复杂的项目可能需要它。不过,从广义上讲,您将拥有一个与模型和一个或多个视图关联的控制器,并且该组对象协同工作以提供一项功能。
If I understand your question -- and I may not, see my comments on it -- I think you're applying the MVC design pattern far too granularly. Most commonly in the setup you describe you'll have a single Model, a single Controller, and multiple Views that are grouped/combined, as in a
.xib
file.In Cocoa Touch terms you'd have one UIView that contains a UILabel with the date and a UITableView for your table. These are your Views.
You'll certainly have a Model for the table data, likely an array of data. Your date data might be from its own model if it's a date retrieved from something or calculated or whatever, something entirely separate from the array of data. If it's instead associated with the array data -- they're both pulling from a database, or the date is calculated from the array data, or what have you -- then you have a single Model.
If the data is all coming from a single Model then a single Controller is likely fine. Even if the data is coming from more than one source/Model you likely only need/want one controller in this setup. The UITableView will have a UITableViewController, and that same controller can take care of providing your date as well.
To sum, the Model View Controller design pattern doesn't call for having a bunch of nested sets of models, views, and controllers. They could be, and sufficiently complex projects may call for it. Broadly, though, you'll have a controller that's associated with a model and one or more views, and that set of objects works together to provide a piece of functionality.
Tbee,
我将在这里发布一个小代码示例,因为看来您并没有真正理解它。
在 InterfaceBuilder 中将它们连接起来,您就得到了一个可用的“MVC”。您不需要为每个按钮使用全新的 UIViewController。视图负责处理它。
UITableView
及其关联的 View 更加复杂,可能需要额外的 UIViewController 来帮助封装。我真的不建议从使用它们开始,但这是一个 这里有很好的教程。 它有 大量图像将向您展示如何在IB等中将事物连接起来。它很旧,所以你的 XCode 可能看起来不像图像,但它有帮助。Tbee,
I'll post a tiny code example here, since it seems you're not really getting it.
Connect those up in InterfaceBuilder, and you've got a working "MVC." You don't need a completely new UIViewController for each button. The one for the View takes care of it.
UITableView
and it's associated Views are more complex, and may require an additional UIViewController to help encapsulate. I really don't suggest starting out by using them, but this is a good tutorial here. It's got a lot of images which will show you how to connect things up in IB and the like. It's old, so your XCode may not look like the images, but it helps.感谢您提供的链接,我会研究一下。
到目前为止,我已经将大部分应用程序重写为使用视图而不是视图控制器(顶级视图控制器除外),并且它开始与可用的 API 调用(如layoutSubviews)相匹配。让我感到不安的是,我现在需要这样做:
在我之前的设置中,我所做的只是:
但显然这就是做到这一点的方法。我不清楚 ATM 机有一件事。由于我在 AppViewController 的 loadView 中构造和布局视图,因此如果方向发生变化,它们如何重新布局。 VC没有layoutSubviews,所以我应该使用didRotateFromInterfaceOrientation并从那里重新定位子视图?
顺便说一句,我不会混合注册匿名内部类作为侦听器(观察者)。我在编写 Swing 组件和 JavaFX 控件方面非常有经验。这可能是罪魁祸首,在 Java(FX) 中,每个组件都有一个视图和一个控制器(并不总是模型)。
Thanks for the links, I'll look into them.
So far I've rewritten most of my application to using views instead of viewcontrollers (except the toplevel one) and it starts to match up with the API calls that are available like layoutSubviews. What I find disturbing that I need to do this now:
Where in my previous setup all I did was:
But apparently that is the way to do it. One thing is unclear to me ATM. Since I construct and layout the view in loadView in my AppViewController, how do they get relayouted if the orientation changes. The VC does not have a layoutSubviews, so I should use the didRotateFromInterfaceOrientation and reposition the subviews from there?
BTW, I'm not mixing registering anonymous inner classes as listeners (observers). I'm very experienced with writing Swing components and JavaFX controls. And that probably is the culprit, in Java(FX) every component has a view and a controller (not always a model).
另一个潜在的联系是 WWDC 2010 关于 MVC 的精彩演讲。
http://developer.apple.com/videos/wwdc/2010/
这是会议 116 - iPhone OS 的模型-视图-控制器
该会议充满了关于 MVC 如何真正工作、它为何有效、为什么它好等实用建议。但它也有很多介绍性的内容,可以帮助刚接触这个概念的人理解它。
如果我理解你上面关于 Java Swing 类的句子,你是在谈论响应事件的匿名类吗?如果是这样,那么它们就不是“MVC”,它们是所谓的“观察者”,当它们从视图观察事件时,它们会采取一些操作(通常向控制器发送消息)。 Cocoa Touch 使用目标/操作范例(和委托)来实现这一点。
我还强烈建议您采纳 Matthew 和 Stephen 的建议并编写一堆代码。如果您没有建立直觉基础,那么提出正确的问题(这是获得良好答案所需的大部分问题)是非常困难的。
我确实认为 WWDC 2010 演讲会有帮助。
祝你好运!
One more potential link is the great talk from WWDC 2010 on MVC.
http://developer.apple.com/videos/wwdc/2010/
It is Session 116 - Model-View-Controllr for iPhone OS
The session is chock full of practical advice on how MVC really works, what makes it tick, why it's good. But it also has a lot of intro stuff to help folks new to the concept to wrap their heads around it.
If I understand your sentence on Java's Swing classes above are you talking about the anonymous classes that respond to events? If so those are not "MVC's", they are what is termed 'Observers', when they observe an event from the view they take some action (usually send a message to a controller). Cocoa Touch uses the Target/Action paradigm (and delegation) to achieve this.
I'd also strongly suggest you take Matthew and Stephen's advice and write a bunch of code. If you don't build that base of intuition, asking the right question (which is most of what is needed to get a good answer) is very difficult.
I really think the WWDC 2010 talk will help.
Good Luck!