【Objective-C】どのクラスからでも現在表示されているUIActionSheetを削除する方法【Xcode10.2対応】

こういう人に向けて発信しています。
・UIActionSheetを任意のタイミングでどこのクラスからでも削除したい人
・レガシーなUIActionSheetを改修する必要が生まれた人
・Objective-C中級者

詳しくは前回の記事を参照。

CustomActionSheet.h/m

//hファイル
#import <UIKit/UIKit.h>

@interface CustomActionSheet : UIActionSheet
- (id)init;

@end



//mファイル
#import "CustomActionSheet.h"
#import "AppDelegate.h"

@implementation CustomActionSheet

//オーバーライズしている
- (id)init{
   self = [super init];
   if (self) {
       AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
       appDelegate.showActionSheet = self;
       
   }
   return self;
}


@end

AppDelegate.h/m

/
#import <UIKit/UIKit.h>
#import "CustomActionSheet.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic) CustomActionSheet *showActionSheet; //


@end




//mファイル
//
//  AppDelegate.m
//  root_projectFile
//
//  Created by iga34 on 2019/04/23.
//  Copyright © 2019 iga34engineer. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   // Override point for customization after application launch.
   return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
   NSLog(@"applicationWillResignActive");
   if(self.showActionSheet){
       [self.showActionSheet dismissWithClickedButtonIndex:-1 animated:NO];
       self.showActionSheet = nil;
   }
}


//アプリが非Activeになる直前に呼び出される
- (void)applicationDidEnterBackground:(UIApplication *)application {
   // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
   // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
   // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
   NSLog(@"applicationDidEnterBackground");

}


- (void)applicationDidBecomeActive:(UIApplication *)application {
   // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
   // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end

ViewController.h/mファイル

//hファイル
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (nonatomic) UIAlertView *alert;


@end


//mファイル
#import "ViewController.h"
#import "AppDelegate.h"
#import "CustomActionSheet.h"

@interface ViewController ()

@end

@implementation ViewController

//testDemo
- (void)viewDidLoad {
   [super viewDidLoad];
}

- (IBAction)buttonTapEvent:(UIButton *)sender {
   CustomActionSheet *as = [[CustomActionSheet alloc] init];
   as.title = @"選択してください。";
   [as addButtonWithTitle:@"実行"];
   [as addButtonWithTitle:@"やめとく"];
   [as addButtonWithTitle:@"キャンセル"];
   as.cancelButtonIndex = 2;
   as.destructiveButtonIndex = 0;
   [as showInView:self.view];  // 
   
}

@end

なぜ今回はinitをオーバーライドしているのか

前回記事ではshowでオーバーライドしましたが、
ActionSheetは表示する時に絶対initを通るのでこれにしました。
initWithTitle的なメソッドがない故にです。


この記事が気に入ったらサポートをしてみませんか?