Objective-C:具有一种模型和多种视图控制器的 MVC

发布于 2024-10-14 14:25:33 字数 1091 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

想念有你 2024-10-21 14:25:33

您可以从功能编程的连接规则 并让根控制器将Brain引入或赋予从属控制器。

简介:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  <#DetailViewController#> *detailViewController 
      = [[<#DetailViewController#> alloc] 
              initWithNibName:@"<#Nib name#>" 
                       bundle:nil];
  // introduce the Brain
  detailViewController.brain = ref.to.rootController.brain;
  // ...
  // Pass the selected object to the new view controller.
  [self.navigationController pushViewController:detailViewController animated:YES];
  [detailViewController release];

捐赠:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  <#DetailViewController#> *detailViewController 
      = [[<#DetailViewController#> alloc] 
              initWithNibName:@"<#Nib name#>" 
                       bundle:nil 
                        brain:ref.to.rootController.brain];
  // ...
  // Pass the selected object to the new view controller.
  [self.navigationController pushViewController:detailViewController animated:YES];
  [detailViewController release];

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:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  <#DetailViewController#> *detailViewController 
      = [[<#DetailViewController#> alloc] 
              initWithNibName:@"<#Nib name#>" 
                       bundle:nil];
  // introduce the Brain
  detailViewController.brain = ref.to.rootController.brain;
  // ...
  // Pass the selected object to the new view controller.
  [self.navigationController pushViewController:detailViewController animated:YES];
  [detailViewController release];

Endowment:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  <#DetailViewController#> *detailViewController 
      = [[<#DetailViewController#> alloc] 
              initWithNibName:@"<#Nib name#>" 
                       bundle:nil 
                        brain:ref.to.rootController.brain];
  // ...
  // Pass the selected object to the new view controller.
  [self.navigationController pushViewController:detailViewController animated:YES];
  [detailViewController release];
我们只是彼此的过ke 2024-10-21 14:25:33

我不确定我理解你的问题....但我尝试:)

为什么你不只是在你的第二个控制器中为模型类定义一个属性并在第一个控制器的 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.

無心 2024-10-21 14:25:33

您可以尝试直接在 AppDelegate 上添加此引用,使其像一个全局变量。

即:

在 AppDelegate 接口中:

Brain *cerebro;

@property (nonatomic, keep) Brain *cerebro;

以及代码中的任何位置:

[(MyAppDelegate *) [[UIApplication sharedApplication] delegate] cerebro]

应该可以很好地工作...


如果您使用多次,并且只有一个 Brain 考虑实现一个单例

如果您使用许多 Brain 实现一个单例 BrainManager 可能是个好主意,您可以像这样使用:

Brain *cerebro = [[Brain alloc] init...]
[[BrainManager sharedManager] addBrain:cerebro withIdentifier:@"cerebro"];

以及其他地方:

[SomeThing DoTaskWithBrain:[[BrainManager sharedManager] brainWithIdentifier:@"cerebro"]];

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 singleton

If you use many Brain it might be a good idea to implement a singleton BrainManager you could use like this :

Brain *cerebro = [[Brain alloc] init...]
[[BrainManager sharedManager] addBrain:cerebro withIdentifier:@"cerebro"];

And somewhere else :

[SomeThing DoTaskWithBrain:[[BrainManager sharedManager] brainWithIdentifier:@"cerebro"]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文