【问题标题】:Pass NSArrays from ViewController to NSObject class将 NSArrays 从 ViewController 传递给 NSObject 类
【发布时间】:2013-02-25 17:53:37
【问题描述】:

我有一个单视图应用程序,其中 ViewController 包含一个自定义类 CPTGraphHostingView 的 UIView,因此它包含一个 CorePlot 图。该 UIView 的内容由名为 SimpleScatterPlot 的 NSObject 子类管理。这个SimpleScatterPlot 对象包含配置图形所需的所有参数(轴范围、标签、点数等)

ViewController.h

#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
#import "SimpleScatterPlot.h"

@interface ViewController : UIViewController
{
    IBOutlet CPTGraphHostingView *_graphHostingView;
    SimpleScatterPlot *_scatterPlot;   
    NSMutableData *dataConexion;
}

@property (nonatomic,strong) NSArray *valor;
@property (nonatomic,strong) NSArray *strAnoMes;
@property (nonatomic,strong) NSArray *indicador;
@property (nonatomic, retain) SimpleScatterPlot *scatterPlot;

@end

在 ViewController.m 中,我有一些方法可以从一些 JSON 数据中初始化一些数组:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize indicador,valor,strAnoMes;

- (void)viewDidLoad
{
    [super viewDidLoad];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    NSString *urlString = @"http://xxxx/ios/datosgrafica.php";

    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    (void)[[NSURLConnection alloc] initWithRequest:request delegate:self];   
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    dataConexion = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
    [dataConexion appendData:theData];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    indicador = [NSJSONSerialization JSONObjectWithData:dataConexion options:nil error:nil];

    // ARRAYS OF INTEREST
    strAnoMes = [indicador valueForKey:@"StrAnoMes"];
    valor = [indicador valueForKey:@"Valor"];

    self.scatterPlot = [[SimpleScatterPlot alloc] initWithHostingView:_graphHostingView andData:pointArray];
    [self.scatterPlot initialisePlot];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The download could not complete - please make sure you're connected to either 3G or Wi-Fi." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [errorView show];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

SimpleScatterPlot.m中我想使用数组“valor”来配置坐标轴范围,我想使用数组“strAnoMes”在xAxis中绘制自定义标签。

我需要做什么才能将 ViewController 中定义的数组传递给 SimpleScatterPlot?

我曾尝试#import "ViewController.h",但它给了我错误。还有其他想法吗?处理这种情况的最佳方法是什么?

【问题讨论】:

    标签: objective-c cocoa-touch nsarray core-plot


    【解决方案1】:

    您可以在 SimpleScatterPlot 中获取两个属性。

    SimpleScatterPlot.h

    @property (nonatomic, retain) NSArray * strAnoMes;
    @property (nonatomic, retain) NSArray * valor;
    

    SimpleScatterPlot.m

    @synthesize strAnoMes;
    @synthesize valor;
    

    在 ViewController.m 中,在 connectionDidFinishLoading: 中创建 scatterPlot 后,将值分配给上述属性,如下所示。

    self.scatterPlot.strAnoMes = strAnoMes;
    self.scatterPlot.valor = valor;
    

    【讨论】:

    • 我正在按照你的建议做,但是 SimpleScatterPlot 中的数组是空的。
    • 在将数组分配给 SimpleScatterPlot 时是否记录了值。
    • 没关系!我刚刚将代码放置在 scatterPlot 的创建之上几行。问题解决了!谢谢!
    【解决方案2】:

    一种方法是创建一个简单的数据对象并将其直接传递给您的SimpleScatterPlot 对象:

    数据对象:

    @interface SimpleScatterPlotData : NSObject
    @property (...) NSArray *valor;
    @property (...) NSArray *strAnoMes;
    @end
    
    @implementaton SimpleScatterPlotData
    @synthesize valar;
    @synthesize strAnoMes;
    -(void)dealloc
    {
       ...
       ...
       [super dealloc];
    }
    @end
    

    SimpleScatterPlot 类中实现加载方法:

    @interface SimpleScatterPlot
    -(void)loadData:(SimpleScatterPlotData *)data;
    @end
    
    @implementation SimpleScatterPlot
    -(void)loadData:(SimpleScatterPlotData *)data
    {
        NSArray *valor = data.valor;
        NSArray *strAnoMes = data.strAnoMes;
        /*
          Do something
        */
    }
    @end
    

    然后在你的ViewController 类中:

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        indicador = [NSJSONSerialization JSONObjectWithData:dataConexion options:nil error:nil];
    
        // ARRAYS OF INTEREST
        strAnoMes = [indicador valueForKey:@"StrAnoMes"];
        valor = [indicador valueForKey:@"Valor"];
    
        SimpleScatterPlotData *data = [[[SimpleScatterPlotData alloc] init] autorelease];
        data.valor = valor;'
        data.strAnoMes = strAnoMes;
    
        self.scatterPlot = [[SimpleScatterPlot alloc] initWithHostingView:_graphHostingView andData:pointArray];
        [self.scatterPlot loadData:data];
    }
    

    【讨论】:

      【解决方案3】:

      您还可以更改当前初始化程序或向 SimpleScatterPlot 类添加新初始化程序,以便在分配对象时传递这些数组。所以你对初始化程序的调用看起来像:

      self.scatterPlot = [[SimpleScatterPlot alloc] initWithHostingView:_graphHostingView andData:pointArray andValor:valor andstrAnoMes:strAnoMes];
      

      然后在您的初始化程序中,您可以将对象属性设置为传入的值。

      【讨论】:

        猜你喜欢
        • 2011-08-17
        • 1970-01-01
        • 2018-07-13
        • 2010-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-21
        相关资源
        最近更新 更多