iOS MVC - 如何将数据从模型传递到控制器? [英] iOS MVC - How to pass data from model to controller?

查看:550
本文介绍了iOS MVC - 如何将数据从模型传递到控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此做了很多研究,但我对我的问题有一个心理障碍。我正在为iOS应用程序开发Objective-C

I did quite a bit of research on this, but I am having a mental block about my problem. I am working on Objective-C for an iOS app

这是我的设置:


  1. 视图控制器从视图中获取文本(用户输入),并将该文本传递给模型的MethodA。

  1. The view controller gets a text from the view (user input), and passes that text to the MethodA of the model.

模型中的MethodA处理输入文本并获取输出(例如,搜索谷歌搜索该文本)。它使用dispatch_async方法进行搜索,该方法将选择器调用到模型中的MethodB。

The MethodA in model works on the input text and gets an output (e.g. searches google for that text). It does the search using dispatch_async method which calls the selector to MethodB within model.

MethodB解析输出并将所有结果放入NSMutableArray

MethodB parses the output and puts all the results into a NSMutableArray

我的问题在这里:如何将NSMutableArray传递回视图控制器,以便我可以在视图中显示它?

My Question Here: how do I pass that NSMutableArray back to view controller so I can show it on the view?

很抱歉,如果我的问题的答案非常简单/明显。我是Objective-C的新手

Sorry, if the answer to my question is very simple/obvious. I am new to Objective-C

推荐答案

我想传递一个委托对象,该对象重新指向一个选择器方法并调用它处理数据的方法将是实现程序应得的loosley耦合结构的好方法。您熟悉这个概念,还是我会为您挖掘一些代码样本?

I guess passing along a delegate-object that respoons to a selector-method and calling this method with the processed data will be a good way to achieve the loosley coupled structure your program deserves. Are you familiar with this concept, or shall I dig up some code-samples for you?

更新:代码示例

因此,我可能会使用调用类,比如 MyViewController 来实现callbackMethod, myCallbackMethod 如下:

So, I would probably use the calling class, say MyViewController, to implement the callbackMethod, myCallbackMethod as follows:

-(void) myCallbakcMethod: NSMutableArray* array {
    //DoWhatever with the returned data
}

重点是在计算完成时将结果传递回此方法。
所以在 MyViewController 中,你调用 MethodA ,你传递一个对委托的引用来处理结果,namly self

The point is to get the result passed back to this method when the computation is finished. So in your MyViewController where you call MethodA you pass along a reference to the delegate to handle the result, namly self.

//From this:
[MyModel methodA:param1]
//To:
[MyModel methodA:param1:self]

MyModel s methodA methodB 将需要添加一个参数(id)delegate 并在调用之间传递它。

MyModels methodA and methodB would need to add a parameter (id)delegate and pass that along between the calls.

In methodB 其中数据 myArray 已准备就绪,请执行以下调用:

In methodB where the data myArray is ready, do the following call:

    if([delegate respondsToSelector:@selector(myCallbackMethod:)]])
        [observer performSelector:@selector(myCallbackMethod:) withObject:myArray];

这篇关于iOS MVC - 如何将数据从模型传递到控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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