【问题标题】:How to pass data (arrays) between functions如何在函数之间传递数据(数组)
【发布时间】:2015-12-19 18:14:11
【问题描述】:

我必须使用s7graphview 库来绘制简单的直方图,并且我有一个名为的自定义函数 -(IBAction)histogram:(id)sender;。在这个函数中,图像中的每个像素都以 RGB 表示形式传递给数组。然后计算像素,我有红色、绿色和蓝色阵列。我可以发送到 NSLog 或其他东西,但问题是,当我尝试将 3 个数组发送到 - (NSArray *)graphView:(S7GraphView *)graphView yValuesForPlot:(NSUInteger)plotIndex; 时。这两个函数都在同一个 .m 文件中,我不知道如何在它们之间传递数据,因为当我编写 redArray 时,Xcode 不建议我使用这个名称。

【问题讨论】:

  • Xcode 并不总是(正确地)提示您。如果这是您自己的函数,您可以添加更多参数以传递更多数据。 (顺便说一句,您的帖子几乎无法理解——您的问题充其量也不清楚。)
  • - (NSArray *)graphView:(S7GraphView *)graphView yValuesForPlot:(NSUInteger)plotIndex; 不是我的职责。这是委托功能。
  • 您需要找到一种方法来允许该委托方法查看您的三个数组。您是否尝试过将您的三个数组变成 ivars 并从该 graphView 委托方法中访问它们?
  • 如果您编写了代码,您可以拥有实现访问权限,例如单独设置的属性。

标签: objective-c arrays s7graphview


【解决方案1】:

由于- (NSArray *)graphView:(S7GraphView *)graphView yValuesForPlot:(NSUInteger)plotIndex 是一个委托方法,它应该在您的类中实现,该类冒充S7GraphView 对象的委托。您没有显式调用,而是在 .m 实现中这样定义它:

- (NSArray *)graphView:(S7GraphView *)graphView yValuesForPlot:(NSUInteger)plotIndex
{
    if ( plotIndex == <some index value> )
        return redArray;
    else
        return nil;
}

我不知道plotIndex 与你的各种颜色数组对应的是什么,但你应该明白了。

S7GraphView 对象需要该数据时,它将调用delegate 方法。

这与实现UITableViewDelegateUITableViewDataSource 方法没有什么不同。当调用UITableView 方法-reloadData 时,它将调用您的视图控制器(假设它是表的委托/数据源)以提供UITableViewCell 对象通过

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = <... dequeue or created ... >.

    /* 
        do some cell set up code based on indexPath.section and indexPath.row
    */

    return cell;
}

我确定与S7GraphView 类似(我没有 API 可以查看它所做的一切)。在您的 IBAction 方法中,您可能会执行以下操作:

- (IBAction)histogram:(id)sender
{
    // maybe you recalculate your red, green, and blue component arrays here and cache
    // or maybe you calculate them when requested by the delegate method

    // tell the S7GraphView it needs to update
    // (not sure what the reload method is actually called)
    [self.myS7GraphView reloadGraph];
}

【讨论】:

    猜你喜欢
    • 2018-04-27
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    • 2017-01-09
    相关资源
    最近更新 更多