【问题标题】:Objective-C: MVC with one model and various viewcontrollersObjective-C:具有一个模型和各种视图控制器的 MVC
【发布时间】:2011-01-26 20:14:54
【问题描述】:

我有一个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 来控制当用户点击第一个viewcontrollertableview 中的一行时显示的详细视图:

- (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 中再次实例化它?

【问题讨论】:

    标签: ios objective-c cocoa-touch model-view-controller


    【解决方案1】:

    您可以从connectivity rules of capabilities programming 中获取一个页面,并让根控制器将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];
    

    【讨论】:

    • @outis - ref.to 语法是什么?
    【解决方案2】:

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

    为什么你不只是在你的第二个控制器中为模型类定义一个属性并设置它,例如在您的第一个控制器的 viewDidLoad 期间。

    或者您在所有控制器中都有对数据模型类的引用。 AppDelegate 是数据模型管理的好地方。

    如果您基于 Xcode 的“基于拆分视图的应用程序”模板创建应用程序,您会得到一个很好的示例。

    【讨论】:

      【解决方案3】:

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

      即:

      在 AppDelegate 界面中:

      Brain *cerebro;

      @property (nonatomic, retain) 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"]];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-12
        • 2013-09-19
        • 1970-01-01
        • 2012-11-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多