0

I have RootViewController where I create my MainViewDownload instance and call method of that instance.

MainViewDownload *download = [[MainViewDownload alloc] init];
[download loadMainViewImages];

How can I know when loadMainViewImages is finished ? I only call loadMainViewImages from RootViewController but loadMainViewImages calls another method inside MainViewDownload class (lets say method2), and that method2 calls again method3. So, is there a way to know when loadMainViewImages is finished (actually when method3 is finished since its last called).

tshepang
  • 12,111
  • 21
  • 91
  • 136
zhuber
  • 5,364
  • 3
  • 30
  • 63

1 Answers1

1

If you're not multithreading, i.e. starting a method that runs on a separate thread from loadMainViewImages, then the methods will be executed sequentially. So once the loadMainViewImages returns you can be sure everything "in it" was executed. This is how methods work.

EDIT for better formatting of the comments:

MainViewDownload.h

@protocol MainViewDownloadDelegate;

@interface MainViewDownload
@property (nonatomic, weak) NSObject<MainViewDownloadDelegate> *delegate;
@end

@protocol MainViewDownloadDelegate
- (void)downloadDidFinish:(MainViewDownload *)download;
@end

MainViewDownload.m

@implementation MainViewDownload

- (void)someMethodThatDownloadsStuff_OrIsCalledAfterTheDownload {
    ...
    if ([self.delegate respondsToSelector:@selector(downloadDidFinish:)]) {
        [self.delegate downloadDidFinish:self];
    }
}

@end

RootViewController.h

@interface RootViewController <MainViewDownloadDelegate>
...
@end

RootViewController.m

@implementation
...
- (void)downloadDidFinish:(MainViewDownload *)download {
    // hide the download view here.
}
@end

Make sure to set the delegate of the download view to the root view controller.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • is there a way to block before loadMainViewImages is finished ? – zhuber May 18 '13 at 14:02
  • What do you mean? Just add something to the end of it. But that will be the same as if you put that code after `loadMainViewImages` in your root view controller. – DrummerB May 18 '13 at 14:03
  • So, I need [download loadMainViewImages] and when loading is done, then remove LoadingView screen for example. But if I just add that line below it wont wait loadMainViewImages to be finished – zhuber May 18 '13 at 14:06
  • This is precisely the case where you might want to consider using a delegate. Add a protocol to your `MainViewDownload` called `MainViewDownloadDelegate` or similar. Add a method `- (void)downloadFinished:(MainViewDownload *)download;` to it. Add a weak delegate property to MainViewDownload that implements this protocol: `@property (nonatomic, weak) NSObject *delegate;`. Extend `RootViewController` to implement the protocol. `@interface RootViewController ` and implement the method in `RootViewController` to do what you want adter a download. – DrummerB May 18 '13 at 14:14
  • Now you just need to send that message to your delegate when the download is done: `if ([self.delegate respondsToSelector:@selector(downloadFinished:)]) [self.delegate downloadFinished:self];` – DrummerB May 18 '13 at 14:15
  • Thanks. Just one more question. How can I add view over navigationcontroller ? I want to add subview (image saying loading.. or something) over whole navigation view, but when I add subview its always added below navigationBar. – zhuber May 18 '13 at 14:26
  • I recommend asking that as a separate question. But there is a good question you'll find a solution if you search, as this is a common problem. – DrummerB May 18 '13 at 14:31