简体   繁体   English

我想在NSObject类中添加MBProgressHUD

[英]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. 尽管可以使用块,KVO或通知,但是最常见的一种是使用委托模式。

You should start by creation a protocol, so you can communicate between your UIViewController and your NSObject that makes the calls. 您应该首先创建一个协议,以便可以在UIViewController和进行调用的NSObject之间进行通信。 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 . 在您的NSObject内部,对于符合协议CommunicationDelegate的对象,您将拥有weak引用。 In your .h you should have something like this: 在您的.h文件中,您应具有以下内容:

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

Just before you actually start your work with the NSObject , you should do: 在实际开始使用NSObject ,您应该执行以下操作:

myObjectThatWillDoSomething.communicationDelegate = self;

At this moment you have a valid reference between your UIViewController and your NSObject . 目前,您的UIViewControllerNSObject之间具有有效的引用。 Inside your UIViewController's .h file, add this: 在您的UIViewController的.h文件中,添加以下内容:

@interface myViewController : UIViewController <CommunicationDelegate>

So your UIViewController complies to the CommunicationDelegate protocol. 因此,您的UIViewController符合CommunicationDelegate协议。

You can now start your MBProgressHUD from your UIViewController . 现在,您可以从UIViewController启动MBProgressHUD Once your NSObject has done his work, you can either call: NSObject完成工作后,您可以调用:

[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 . 一旦调用了这些方法之一,就可以删除MBProgressHUD Understand that this methods are called inside your UIViewController . 了解此方法在UIViewController内部调用。

When you upload your NSObject, a view is displayed, right? 当您上传NSObject时,会显示一个视图,对吗? So display your HUD in that view. 因此,在该视图中显示HUD。 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 . 使用NSNotification Center停止指示器,在视图控制器中用监听方法声明NSNotification。 And post notification from Webservice file . 并从Webservice文件中发布通知。 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. 将相关的View或ViewController设置为上载者委托,并触发MBProgressHUD在此处添加/删除。

These Below Code Works fine in NSObject File 这些下面的代码在NSObject文件中工作正常

Step 1: Download the 步骤1:下载 https://github.com/jdg/MBProgressHUD https://github.com/jdg/MBProgressHUD

Step 2: Add the Delegate MBProgressHUDDelegate 步骤2:添加代理MBProgressHUDDelegate

Step 3: Declare Instance MBProgressHUD *HUD; 步骤3:声明实例MBProgressHUD * HUD;

Step 4: Write Code Where u want: 步骤4:在需要的地方编写代码:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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