简体   繁体   English

如何将AFNetworking与多个UIViewControllers一起使用?

[英]How to use AFNetworking with multiple UIViewControllers?

For example, one UIViewController requests data using AFNetworking on appear event and perform some actions on result receiving. 例如,一个UIViewController在出现事件时使用AFNetworking请求数据,并对结果接收执行一些操作。 These actions are defined in a block which I give to a request object as a param. 这些动作在一个块中定义,我将其作为参数提供给请求对象。

Now I want to add a button which will push another view controller. 现在,我想添加一个按钮,它将按下另一个视图控制器。 This view controller will use the same data which the first view controller already uses. 该视图控制器将使用第一个视图控制器已经使用的相同数据。

The problem is I don't need to resend the same request for the second one but I'll have some issues with it: 1)I can't set its params directly on request completion because the second view controller may not exist yet 2)If the second one is currently displayed then should I handle the data in the first one? 问题是我不需要为第二个请求重新发送相同的请求,但是会遇到一些问题:1)我无法在请求完成时直接设置其参数,因为第二个视图控制器可能还不存在2 )如果当前显示第二个数据,那么我应该处理第一个数据吗?

To handle requests I create NSURLSessionTask object and call showAlertViewForTaskWithErrorOnCompletion:delegate: . 为了处理请求,我创建了NSURLSessionTask对象并调用showAlertViewForTaskWithErrorOnCompletion:delegate: It is just two strings of code so I think it is not necessary to post it. 它只是两个代码字符串,因此我认为没有必要发布它。

Fundamentally it sounds like you are trying to share data between view controllers. 从根本上来说,这听起来像您正在尝试在视图控制器之间共享数据。 There are multiple solutions to this problem, refer to this answer: 有多种解决方案,请参考以下答案:

Passing Data between View Controllers 在视图控制器之间传递数据

For checking which is the displayed view controller, consult this answer: 要检查哪个是显示的视图控制器,请参考以下答案:

How to determine which view controller is currently active/the one displaying a view? 如何确定哪个视图控制器当前处于活动状态/哪个显示视图?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM