Objective-C:具有一种模型和多种视图控制器的 MVC
我有一个 RootViewController
(基于导航的应用程序),它要求模型 (brain.h/m
) 执行和检索一些信息。显然我首先实例化了一个模型变量。
这是 RootViewController.h
界面:
#import <UIKit/UIKit.h>
#import "Brain.h"
@interface RootViewController : UITableViewController
{
Brain *cerebro;
}
@property (nonatomic, retain) Brain *cerebro;
@end
我添加了第二个 viewcontroller
来控制当用户点击 tableview< 中的一行时显示的详细视图第一个
viewcontroller
的 /code>:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
如何引用并询问第一个 viewcontroller
已实例化的模型,而无需在第二个 viewcontroller 中再次实例化它
?
I have a RootViewController
(navigation based application) that ask a model (brain.h/m
) to perform and retrieve some information. Obviously I've instantiated a model variable first.
This is the RootViewController.h
interface:
#import <UIKit/UIKit.h>
#import "Brain.h"
@interface RootViewController : UITableViewController
{
Brain *cerebro;
}
@property (nonatomic, retain) Brain *cerebro;
@end
I've added a second viewcontroller
to control a detailed view that is displayed when the user taps a row in the tableview
of the first viewcontroller
:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
How can I reference and ask the model that the first viewcontroller
had instantiated without instantiate it again in the second viewcontroller
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以从功能编程的连接规则 并让根控制器将
Brain
引入或赋予从属控制器。简介:
捐赠:
You can take a page from the connectivity rules of capabilities programming and have the root controller introduce or endow the
Brain
to the subordinate controller.Introduction:
Endowment:
我不确定我理解你的问题....但我尝试:)
为什么你不只是在你的第二个控制器中为模型类定义一个属性并在第一个控制器的 viewDidLoad 期间设置它。
或者您在所有控制器中都有对数据模型类的引用。 AppDelegate 是数据模型管理的一个好地方。
如果您基于 Xcode 的“基于拆分视图的应用程序”模板创建应用程序,您将得到一个很好的例子。
I am not sure I understood your question .... but i try :)
Why you not just define a property in your second controller for the model class(es) and set it e.g. during the viewDidLoad of your first controller.
Or you have a reference in all your controllers to you data model classes. A good place for data model management is the AppDelegate.
If you create a application based on the "Split View based Application" template of Xcode you get a good example.
您可以尝试直接在 AppDelegate 上添加此引用,使其像一个假全局变量。
即:
在 AppDelegate 接口中:
Brain *cerebro;
@property (nonatomic, keep) Brain *cerebro;
以及代码中的任何位置:
[(MyAppDelegate *) [[UIApplication sharedApplication] delegate] cerebro]
应该可以很好地工作...
如果您使用多次,并且只有一个
Brain
考虑实现一个单例如果您使用许多
Brain
实现一个单例BrainManager
可能是个好主意,您可以像这样使用:以及其他地方:
You can try adding this reference directly on AppDelegate to make it like a fake global var.
ie :
in the AppDelegate interface :
Brain *cerebro;
@property (nonatomic, retain) Brain *cerebro;
And anywhere in the code :
[(MyAppDelegate *)[[UIApplication sharedApplication] delegate] cerebro]
should work well...
If you use many times, and only one
Brain
think about implementing a singletonIf you use many
Brain
it might be a good idea to implement a singletonBrainManager
you could use like this :And somewhere else :