在视图控制器之间传递数组? [英] Passing array between view controllers?

查看:93
本文介绍了在视图控制器之间传递数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的需要更多帮助!

我正在尝试将数组从一个视图控制器传递到另一个视图控制器。我认为后者是一个'子'视图控制器?

I am trying to pass an array from one view controller to another. I think the latter being a 'child' view controller?

我的代码如下:

MainViewController。 h:

MainViewController.h:

#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>

@interface HelloWorldIOS4ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, AVAudioPlayerDelegate> {
    NSMutableArray  *countProductCode;
    UIPopoverController *detailViewPopover;
}

@property (nonatomic, retain) NSMutableArray  *countProductCode;

@property (nonatomic, retain) UIPopoverController *detailViewPopover;

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
...
@end

MainViewController.m

MainViewController.m

#import "HelloWorldIOS4ViewController.h"
#import "JSON.h"
#import "PopoverContentViewController.h"

@implementation HelloWorldIOS4ViewController

@synthesize detailViewPopover;
@synthesize countProductCode;

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
    NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSDictionary *results = [jsonString JSONValue];
    NSLog(@"RETURN: %@", results);

    [countProductCode removeAllObjects];

    NSArray *products = [results objectForKey:@"items"];

    for (NSDictionary *row in products)
    {
        NSString *code = [row objectForKey:@"ic"];
        [countProductCode addObject:code];
    }

    PopoverContentViewController.countProductCodes = countProductCode;
}       

PopoverViewController.h:

PopoverViewController.h:

@interface PopoverContentViewController : UITableViewController {
    NSMutableArray  *countProductCodes;
}
@property (nonatomic, retain) NSMutableArray  *countProductCodes;
@end

PopoverViewController.m:

PopoverViewController.m:

#import "PopoverContentViewController.h"
#import "HelloWorldIOS4ViewController.h"

@implementation PopoverContentViewController

@synthesize countProductCodes;
...

我已经削减了很多,但我知道从NSLog点缀了我得到的数据等,但我无法将数组 countProductCode 传递给 PopoverViewController的 countProductCodes 数组。

I have cut a lot out, but I know from a load of NSLog's dotted around that I get the data back etc, but I cannot pass the array countProductCode to the PopoverViewController's countProductCodes array.

我一直在


访问未知'setCountProductCodes:'类方法'

"Accessing unknown 'setCountProductCodes:' class method"

错误。

这可能是我真正愚蠢的事情,但这让我发疯了!

This may be something really silly that I'm doing but it's driving me crazy!

有人可以帮忙吗?

谢谢
詹姆斯

推荐答案

亲爱的詹姆斯,
我想你想拥有仔细研究模型 - 视图 - 控制器范例。在你的应用程序中,你试图实现某种超级。让我解释一下这意味着什么:

Dear James, I think you would like to have a closer look at the Model-View-Controller paradigm. In your app you are trying to implement some kind of a "super class". Let me explain what that means:

在你的MainViewController类中,它显然是一个控制器,也实现了模型的某些部分。这是一个坏主意,但在开始时是非常常见的。也许我误解了你的设计,但这是我将如何实现它:

In your MainViewController class which is clearly a controller there is also some part of the model implemented. This is a bad idea, but a very common one to make in the beginning. Maybe I misunderstood your design, but here is how I would implement it:

模型我会实现一个合适的模型对象,可能是在你的情况下就像一个自定义的 NSObject 子类一样简单,带有 NSMutableArray 作为属性。此外,该模型将具有从互联网上检索数据的方法。这是对的:在模型中进行网络连接。您必须拥有从控制器调用的 - (void)refreshProductCode 等方法。如果你想变得非常花哨,可以使用 NSOperation 来封装下载(然后你将使用 NSURLConnection的同步变体,因为操作本身已经异步执行了。如果你对JSON字符串的解析需要更长的时间,那么这将在后台执行,你的UI将保持响应。

The Model I would implement a proper model object, which could be in your case as easy as a custom NSObject subclass with a NSMutableArray as a property. In addition this model would have the methods for retrieving data off the internet implemented. That is right: do the networking in the model. You would have to have methods like - (void) refreshProductCode that you would call from your controller. If you want to get really fancy, use an NSOperation to encapsulate the download (then you would use the a synchronous variant of NSURLConnection, because the operation itself is already executed asynchronously) The nice thing would be then if your parsing of the JSON string takes longer, also this is performed in the background and your UI will stay responsive.

所以现在模特正在下载你的东西 - 很棒,但我怎么知道什么时候完成?那么一旦完成,你就会从模型中发布通知。如果下载失败怎么办?您猜对了:发布失败的通知。

So now the model is downloading your stuff - great, but how do I know when it is done? Well you would post a Notification from the model once it is done. What if the download fails? You guessed it right: post a notification that it failed.

控制器需要显示模型数据的控制器首先得到模型对象。在这种情况下,模型对象是AppController上的属性。然后控制器具有该模型对象的属性并保留它,以便模型对象在控制器存在时不会消失。然后,控制器还注册模型的通知。那么典型的下载工作如何呢?

The Controller The controller which needs to display data from the model would first to get the model object. In this case the model object is a property on your AppController. The controller then has a property for this model object and retains it, so that the model object does not go away while the controller lives. The controller then also registers for notifications of the model. So how would a typical download work then?


  1. 获取模型对象的实例

  2. 调用模型对象上的 - (void)refreshProductCode

  3. 在状态栏中显示网络活动微调器并等待通知

  4. 当通知进入时,成功刷新UI并在失败时重新开始下载或向用户显示注释。同时禁用网络活动微调器。

  1. Get an instance of the model object
  2. call -(void) refreshProductCode on the model object
  3. display network activity spinner in status bar and wait for notifications
  4. when the notification came in, on success refresh the UI and on failure restart download or display a note to the user. Also disable the network activity spinner.

如何在视图控制器之间移动数据?视图控制器应该运行有点像黑手党:每个视图控制器都在需要知道的基础上工作。例如,如果您希望视图控制器显示产品的详细信息,则不会将包含所有产品的模型传递给控制器​​。相反,你会在详细视图控制器上有一个实例变量,它只包含一个产品模型对象,它包含描述文本,照片等所有信息。如果你想在你的应用程序中再次显示产品信息,那么很酷可以重用该视图控制器,因为它只需要一个产品模型对象来完成它的工作。

How do you shuffle data between view controllers? View controllers should operate a bit like the mafia: every view controller is working on a need-to-know basis. For example if you want a view controller to display the details of your product, you would not pass the model with all your products to the controller. Instead you would have an instance variable on the detail view controller holding only one produce model object, which has all the information like description text, photo etc. The cool thing then is if you ever want to display product information again in you app, you can reuse that view controller, as all it needs is a product model object to do its job.

这篇关于在视图控制器之间传递数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆