LoginSignup
1
3

More than 1 year has passed since last update.

Objective-C, Swift ライブラリ無しでインジケーターアラートを表示

Last updated at Posted at 2017-12-01

カテゴリで実装していますが、
ViewControllerであればコピペで使えるレベルです。

実装

UIViewController+Alert.m
#import "UIViewController+Alert.h"

@implementation UIViewController (Alert)

- (void)showIndicatorAlert:(NSString*)text
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:text preferredStyle:UIAlertControllerStyleAlert];
    UIActivityIndicatorView *view = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    view.center = CGPointMake(60, 30);
    [alertController.view addSubview:view];
    [view startAnimating];
    [self presentViewController:alertController animated:YES completion:nil];
}

@end
呼び出し元

[self showIndicatorAlert:@"Please wait..."];

表示結果

image.png

閉じる方法

別スレッドとかで

... 処理 ...

// 閉じる
dispatch_sync(dispatch_get_main_queue(), ^{
    [self dismissViewControllerAnimated:true completion:nil];
});

Swift版

extension UIViewController {
    func showIndicatorAlert(text: String) {
        let alert = UIAlertController(title: text,
                                      message: "",
                                      preferredStyle: .alert)
        let indicatorView = UIActivityIndicatorView(style: .medium)
        indicatorView.center = CGPoint(x: 60, y: 30)
        indicatorView.startAnimating()
        alert.view.addSubview(indicatorView)
        self.present(alert, animated: true, completion: nil)
    }
}
  • 表示
self.showIndicatorAlert(text: "Please wait...")
  • 閉じる
self.dismiss(animated: true)
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3