简体   繁体   中英

I want to add MBProgressHUD in NSObject class

我正在从我的NSObject类在服务器上上传数据,现在我想在上传数据时显示MBProgressHUD,我知道如何使用ViewController显示MBProgressHUD,但不知道如何通过NSObject类显示。

AppDelegate* delegate = [[UIApplication sharedApplication] delegate];
[delegate.window addSubview:HUD];
...
[HUD removeFromSuperview];

There are several ways to solve this kind of problems. The most common one is using the delegation pattern, although you can use blocks, KVO, or Notifications.

You should start by creation a protocol, so you can communicate between your UIViewController and your NSObject that makes the calls. Although you don't need one to create this communication, you should use it to have a more flexible code.

Normally, I would do something like this:

@protocol CommunicationDelegate <NSObject>

@required

-(void)communicationSucceed;

-(void)communicationFailedWithError:(NSError*)anError;

Inside your NSObject , you will have a weak reference for an object that complies with the protocol CommunicationDelegate . In your .h you should have something like this:

@property(nonatomic, weak) id <CommunicationDelegate> communicationDelegate;

Just before you actually start your work with the NSObject , you should do:

myObjectThatWillDoSomething.communicationDelegate = self;

At this moment you have a valid reference between your UIViewController and your NSObject . Inside your UIViewController's .h file, add this:

@interface myViewController : UIViewController <CommunicationDelegate>

So your UIViewController complies to the CommunicationDelegate protocol.

You can now start your MBProgressHUD from your UIViewController . Once your NSObject has done his work, you can either call:

[communicationDelegate communicationSucceed];

or

[communicationDelegate communicationFailedWithError:anError]; //anError is used to describe what went wrong

Once (one of) those methods are called, you can remove your MBProgressHUD . Understand that this methods are called inside your UIViewController .

When you upload your NSObject, a view is displayed, right? So display your HUD in that view. You may need to create a delegate to notify the view when the download begin, when it ends and if there is an error.

Use NSNotification Center to stop Indicator , Declare NSNotification with listening method in view controller . And post notification from Webservice file . Stop indicator in listening method of Notification center .

This link will help you

Create a delegate protocol on your uploader object

@protocol UploaderThingyDelegate <NSObject>

-(void)stuffStarted;

-(void)stuffEnded;

@end

@interface UploaderThingy : NSObject

@property (weak) id<UploaderThingyDelegate> delegate;

Set your relevant View or ViewController as the uploaders delegate and trigger the MBProgressHUD add/remove there.

These Below Code Works fine in NSObject File

Step 1: Download the https://github.com/jdg/MBProgressHUD

Step 2: Add the Delegate MBProgressHUDDelegate

Step 3: Declare Instance MBProgressHUD *HUD;

Step 4: Write Code Where u want:

HUD = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWindow];
[[UIApplication sharedApplication].keyWindow addSubview:HUD];

HUD.delegate = self;
HUD.labelText = @"Processing";
[HUD show:YES];

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