简体   繁体   中英

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. 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?

To handle requests I create NSURLSessionTask object and call 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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