全局使用MBProgressHUD +使其成为单身人士 [英] Use of MBProgressHUD Globally + make it singleton

查看:184
本文介绍了全局使用MBProgressHUD +使其成为单身人士的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,每个用户交互事件都进行网络调用(即TCP,而不是HTTP)。我需要活动指标从显示全局到 UIViewController 并隐藏 NetworkActivityManager Class(处理网络活动的自定义类,它不是UIViewController或UIView的子类)。

In my Project, each of the user interaction events make a network call (Which is TCP, not HTTP). I need Activity Indicator to be global to show from a random UIViewController and hide from NetworkActivityManager Class (a custom class to handle network activities, Which is not a subclass of UIViewController or UIView).

搜索网络后我发现了 MBProgressHUD 用于同一目的,但我无法找到一个关于如何全局使用它的示例。 (通过说全局我的意思是 MBProgressHUD 的单个对象以及显示和隐藏它的类方法。)

After searching the web I found out that MBProgressHUD is used for the same purpose, but I wasn't able to find out an example on how would I use it globally. (By saying global I mean a singleton object of MBProgressHUD and class methods to SHOW and HIDE it.)

以下是我尝试过的,但是,失败:
AppDelegate.h中:

Following is what I have tried yet, but, failed: In AppDelegate.h:

@property(非原子,保留)MBProgressHUD * hud;

AppDelegate.m中:

@synthesize hud;

在一些随机的UIViewController对象中:

In some random UIViewController object:

appDelegate.hud = [MBProgressHUD showHUDAddedTo:appDelegate.navigationController.topViewController.view animated:YES];
appDelegate.hud.labelText = @"This will take some time.";

隐藏它时,从 NetworkActivityManager 类:

[MBProgressHUD hideHUDForView:appDelegate.navigationController.topViewController.view animated:YES];

这会使项目在一段时间后崩溃(由于内存问题。)
I我在我的项目中使用ARC,而且,我正在使用ARC版本的 MBProgressHUD

This makes the project to crash after some time (due to memory issues.) I am using ARC in my project and also, I am using the ARC version of MBProgressHUD.

我错过了什么?

重要问题:

我可以让 MBProgressHUD 像UIAlertView一样工作吗? (说我的意思是 MBProgressHUD 的实现独立于UIView - 它使用 showHUDAddedTo:来呈现)???

Can I make MBProgressHUD work like UIAlertView? (Saying that I mean implementation of MBProgressHUD independent of UIView -- sa it uses showHUDAddedTo: to present itself) ???

请注意:在上面隐藏MBProgressHUD的代码中,View可能会与显示MBProgressHUD时的内容有所不同。

Please Note: In the above code of hiding MBProgressHUD, View may be changed from what it was when showing MBProgressHUD.

任何帮助非常感谢。

推荐答案

您可以将此添加到您喜欢的课程中:

You could add this to a class of your liking:

+ (MBProgressHUD *)showGlobalProgressHUDWithTitle:(NSString *)title {
    UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
    hud.labelText = title;
    return hud;
}

+ (void)dismissGlobalHUD {
    UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
    [MBProgressHUD hideHUDForView:window animated:YES];
}

这可以在任何类上调用。使用这些类便捷方法时,您无需对HUD进行强有力的引用。

This can be than called on any class. You don't need to keep a strong reference to the HUD when using those class convenience methods.

根据您的具体情况,您可能还希望处理在隐藏另一个hud之前请求新hud的情况。当新的进入或者出现某种排队等时,你可以食用隐藏之前的hud。

Depending on your specific situation you'll probably also want to handle cases where a new hud is requested before the other one is hidden. You could eater hide the previous hud when a new comes in or come up with some sort of queueing, etc.

在显示新的HUD实例之前隐藏它是很漂亮的直截了当。

Hiding the previous HUD instance before showing a new one is pretty straightforward.

+ (MBProgressHUD *)showGlobalProgressHUDWithTitle:(NSString *)title {
    UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
    [MBProgressHUD hideAllHUDsForView:window animated:YES];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
    hud.labelText = title;
    return hud;
}

这篇关于全局使用MBProgressHUD +使其成为单身人士的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆