Displaying MBProgressHUD in Objective-C: A Step-by-Step Guide

Integrating MBProgressHUD into an NSObject Class

=====================================================

In this article, we will explore how to integrate MBProgressHUD into an NSObject class. MBProgressHUD is a popular iOS library used for displaying progress indicators and notifications in mobile applications.

Introduction to MBProgressHUD


MBProgressHUD is a powerful tool that can be used to display progress indicators, notifications, and alerts in your iOS application. It provides a simple and easy-to-use API for customizing the appearance and behavior of these UI elements.

To get started with MBProgressHUD, you will need to add it to your project using CocoaPods or by downloading the library directly from GitHub. In this article, we will assume that you have already integrated MBProgressHUD into your project.

Displaying MBProgressHUD in an NSObject Class


While MBProgressHUD is commonly used in ViewControllers, it can also be used in other classes such as NSObject to display a progress indicator or notification. However, there are some differences in how you would use MBProgressHUD when displaying a progress indicator in an NSObject class compared to a ViewController.

In this section, we will explore the different ways you can display MBProgressHUD in an NSObject class and provide examples for each approach.

Approach 1: Using the HUD Property


One way to display MBProgressHUD in an NSObject class is by using the HUD property. Here’s an example:

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

In this code snippet, we first get a reference to the delegate object which is typically the AppDelegate of your application. Then, we add the HUD view to the window of the application.

However, as you can see from the above code snippet, there’s no HUD variable declared anywhere in our Objective-C file or elsewhere in our project that we could be adding this to.

To fix this issue, we need to create a new property in our AppDelegate class. Here’s how to do it:

@interface AppDelegate : NSObject <UIApplicationDelegate>

@property (nonatomic, strong) MBProgressHUD *HUD;

@end

@implementation AppDelegate

- (void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Create a new instance of HUD
    self.HUD = [MBProgressHUD showWithCompletion:nil];

    return YES;
}

- (void)dealloc {
    // Remove the HUD from the window before deallocing
    [self.HUD removeFromSuperview];
}

@end

In this updated code snippet, we declare HUD as a property in our AppDelegate class and initialize it when the application finishes launching. We also remove the HUD view from the window when the AppDelegate is deallocated.

Approach 2: Using a Separate Method


Another way to display MBProgressHUD in an NSObject class is by using a separate method that can be called whenever you need to display a progress indicator or notification.

Here’s how you could implement this:

@interface MyClass : NSObject

@property (nonatomic, strong) MBProgressHUD *HUD;

- (void)showProgressHUDWithMessage:(NSString *)message;
- (void)hideProgressHUD;

@end

@implementation MyClass

- (void)showProgressHUDWithMessage:(NSString *)message {
    self.HUD = [MBProgressHUD showWithCompletion:nil message:message];
}

- (void)hideProgressHUD {
    if (self.HUD != nil) {
        [self.HUD removeFromSuperview];
        self.HUD = nil;
    }
}

@end

In this updated code snippet, we create a separate method showProgressHUDWithMessage that takes in the message you want to display as an argument. We also add another method hideProgressHUD to remove the HUD view from the window and set it to nil.

Example Use Case


Here’s how you can use these approaches:

@implementation MyClass

- (void)uploadData {
    // Code for uploading data goes here

    [self showProgressHUDWithMessage:@"Uploading..."];

    // Simulate a long-running operation
    [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(finishUpload) userInfo:nil repeats:NO];

    // Remove the HUD after the upload is complete
    [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(hideProgressHUD) userInfo:nil repeats:YES];
}

- (void)finishUpload {
    [self hideProgressHUD];

    // Code for finishing the upload goes here
}

@end

In this example, we call showProgressHUDWithMessage when the data is being uploaded and remove the HUD after 10 seconds using a timer.

Conclusion


Displaying progress indicators or notifications in an NSObject class can be achieved by using MBProgressHUD. There are different ways to do this depending on your specific use case. In this article, we explored two approaches: using the HUD property and creating separate methods for displaying and hiding the HUD view. We also provided example code snippets for each approach.

I hope you found this tutorial helpful in integrating MBProgressHUD into an NSObject class.


Last modified on 2024-12-18