Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Of course it's possible Will and it has very little to do with AFNetworking but much more to do with common programming patterns.</p> <p>You're going to need to store the <code>AFURLConnectionOperation</code> object outside of your view controllers where they can both access it. The best practice here would be creating a <a href="https://stackoverflow.com/questions/5720029/create-singleton-using-gcds-dispatch-once-in-objective-c">singleton class</a> that encapsulates the AFNetworking properties and methods to handle uploading your image(s). Whenever you need information about or to interact with that upload you can simply access that singleton via a class method like a <code>sharedInstance</code>.</p> <pre><code>+ (id)sharedInstance { static dispatch_once_t once; static id sharedInstance; dispatch_once(&amp;once, ^{ sharedInstance = [[self alloc] init]; }); return sharedInstance; } </code></pre> <p>If you are interacting with a web service (and not a raw FTP server), then subclassing <a href="http://afnetworking.github.com/AFNetworking/Classes/AFHTTPClient.html" rel="nofollow noreferrer">AFHTTPClient</a> would probably be your best bet for the 'upload manager' type of class solution.</p> <p>Whatever you choose, once you have a simple class put together you can then register for KVO notifications in your ViewControllers' viewWillAppear &amp; unregister in viewWillDisappear to cleanly handle your UI updates (e.g. progress bar). If you don't understand Key-Value Observing, read the <a href="http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html#//apple_ref/doc/uid/10000177i" rel="nofollow noreferrer">Introduction to Key-Value Observing Programming Guide</a>. You'll be much more able to cope in iOS after having that knowledge under your belt.</p> <p>So, in view A's upload code, use your magic new class to create and enqueue the upload using a URL (multiple methods could be made to use images in-memory, NSFileURLs or an NSString as shown here)</p> <pre><code>[[MyImageUploadManager sharedInstance] uploadImageFromPath:@"/wherever/1.png" toURL:[NSURL URLWithString:@"ftp://someserver.com/images/"]]; </code></pre> <p>…in View C's controller's viewWillAppear</p> <pre><code>- (void) viewWillAppear:(BOOL)animated { ... [[MyImageUploadManager sharedInstance] addObserver:self forKeyPath:@"progress" options:NSKeyValueObservingOptionNew context:nil]; ... } </code></pre> <p>… and in View C's viewWillDisappear</p> <pre><code>- (void)viewWillDisappear:(BOOL)animated { ... [[MyImageUploadManager sharedInstance] removeObserver:self forKeyPath:@"progress" context:nil]; ... } </code></pre> <p>Whenever that 'progress' property changes in your upload manager class, iOS will call the function <code>observerValueForKeyPath:ofObject:change:context:</code>. Here's what a very simple version of that looks like:</p> <pre><code>-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ( [keyPath isEqualToString:@"progress"] ) { // since you only upload a single file, and you've only added yourself as // an observer to your upload, there's no mystery as to who has sent you progress float progress=[change valueForKey:NSKeyValueChangeNewKey]; NSLog(@"operation:%@ progress:%0.2f", object, progress ); // now you'd update the progress control via a property bound in the nib viewer [[_view progressIndicator] setProgress:progress]; } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } } </code></pre> <p>That should get you well on the way, hope that was helpful.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload