トップ «前の日記(2012-05-27) 最新 次の日記(2012-05-29)» 編集

Cocoa練習帳

iOS/iPhone/iPad/watchOS/tvOS/MacOSX/Android プログラミング, Objective-C, Cocoa, Swiftなど

2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|09|10|11|12|
2024|01|02|03|

2012-05-28 [iOS]アラートのカスタマイズ(2)

アラートの内容をカスタマイズするなら、ビューコントローラにしてnibにしてしまえば?ということで、やってみました。

アラート風のビューコントローラを呼び出すコードだ。

- (IBAction)modalPane:(id)sender
{
    ModalPaneViewController *modalPaneViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ModalPaneViewController"];
    [modalPaneViewController setCompletionHandler:^(ModalPaneViewControllerResult result) {
        switch (result) {
            case ModalPaneViewControllerResultCancelled:
                [self performSelectorOnMainThread:@selector(didCancel:) withObject:nil waitUntilDone:NO];
                break;
            case ModalPaneViewControllerResultDone:
                [self performSelectorOnMainThread:@selector(didDone:) withObject:nil waitUntilDone:NO];
                break;
            default:
                break;
        }
        
        [self dismissModalViewControllerAnimated:YES];
    }];
    [self presentModalViewController:modalPaneViewController animated:YES];
}
 
- (void)didDone:(id)arg
{
    DBGMSG(@"%s", __func__);
}
 
- (void)didCancel:(id)arg
{
    DBGMSG(@"%s", __func__);
}

アラート風ビューコントローラのコードは、以前紹介したModelPaneとなる。

typedef enum ModalPaneViewControllerResult {
    ModalPaneViewControllerResultCancelled,
    ModalPaneViewControllerResultDone
} ModalPaneViewControllerResult;
 
typedef void (^ModalPaneViewControllerCompletionHandler)(ModalPaneViewControllerResult result);
 
@interface ModalPaneViewController : UIViewController
 
@property (nonatomic, copy) ModalPaneViewControllerCompletionHandler    completionHandler;
 
- (IBAction)done:(id)sender;
- (IBAction)cancel:(id)sender;
 
@end
@implementation ModalPaneViewController
 
@synthesize completionHandler = _completionHandler;
 
- (void)viewDidLoad
{
    [super viewDidLoad];
}
 
- (void)viewDidUnload
{
    self.delegate = nil;
    self.completionHandler = nil;
    [super viewDidUnload];
}
 
- (IBAction)done:(id)sender
{
    DBGMSG(@"%s", __func__);
    if (self.completionHandler) {
        self.completionHandler(ModalPaneViewControllerResultDone);
    }
}
 
- (IBAction)cancel:(id)sender
{
    DBGMSG(@"%s", __func__);
    if (self.completionHandler) {
        self.completionHandler(ModalPaneViewControllerResultCancelled);
    }
}
@end
実行

ただし、ビューコントローラは全画面?なので、背景のビューを透明に設定してみたのだが、上手くいかなかった?何故だ!

IB

_ ソースコード

GitHubからどうぞ。
https://github.com/murakami/workbook/tree/master/ios/Dialog - GitHub

_ 関連情報

[iOS] UIAlertView 上に UIProgressView を載せる [2] キャンセルボタン表示
『Cocoaの日々』いつも参考にさせていただいています。ありがとう!助かります。

トップ «前の日記(2012-05-27) 最新 次の日記(2012-05-29)» 編集