0

In the application I build, I've created TerminalController subclass of NSPersistentDocument. In TerminalController there is an outlet of NSProgressIndicator that is connected to instance of progress indicator in main window nib. The Progress Indicator that will serve determinate progress is placed in TerminalController class, because I want all my various classes to use in process of uploading or downloading data from server.

There is especially one class that is used frequently for uploading images - ImageUploader - which suppose to trigger progress indicator located in main window nib and connected to TerminalController.

My problem is I cannot seem to figure out how to access progress indicator in TerminalController from ImageUploader. I can't create instance of ImageUploader in main window nib and connected there, as ImageUploader gets frequently initialised and released in the code.

There must be a way of accessing from any object I would think. One of my idea was to make progress indicator global instance, but I am aware from the structure point of view is wrong and not recommended.

I would really appreciate any suggestions regarding this issue

Here is a sample of my code

TerminalController subclass of NSPersistentDocument

@interface TerminalController : NSPersistentDocument{
    IBOutlet NSProgressIndicator *loadingIndicator;
}
@property (retain, nonatomic) NSProgressIndicator *loadingIndicator;

@implementation TerminalController

@synthesize loadingIndicator;

- (void)dealloc
{
    [loadingIndicator release];
    [super dealloc];
}

ImageUploader class

@interface ImageUploader : NSObject{
    ...

    NSProgressIndicator  *progressIndictor; // This is progress indicator that should point to TerminalController progress indicator
}

@implementation CustomXMLRequests

- (void) uploadImage:(NSURL *)withUrl{
    ...

    // How to point to progress indicator that is located in TerminalController that is subclass of NSPersistentDocument
    [progressIndictor setUsesThreadedAnimation:YES];
    [progressIndictor startAnimation:self];
    [progressIndictor setMinValue:0.0];
    [progressIndictor setMaxValue:1.0];
}

- (void)connection:(NSURLConnection *)connection 
   didSendBodyData:(NSInteger)bytesWritten 
 totalBytesWritten:(NSInteger)totalBytesWritten 
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite{
    double prgrs = (double)totalBytesWritten/totalBytesExpectedToWrite;
    [progressIndictor setDoubleValue:prgrs];
    [progressIndictor displayIfNeeded];
}
PiotrDomo
  • 1,045
  • 10
  • 29

1 Answers1

1

If you can't make a direct XIB connection (via IBOutlet from one view controller to the other), your next best bet is to either use "NSNotifications" to pass along a message to interested observers (i.e. from your ImageUploader to your TerminalController), or you can have your TerminalController somehow register as a delegate to your ImageUploader and then update your progress indicator that way.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215