エンジニアリングにはほど遠い

iPhoneアプリとかサイトとかをつくっていくブログです。

iOS QuickDialogからさらにQuickDialogを開きたい〜!(電波少年風)

QuickDialogのサブクラスを作って手でうちゃうちゃやるのはあまりよろしくなさそうという事で、カテゴリでやってみました。
これが正解のやり方だとは思っていないので、もっといい方法があれば教えて頂きたいです。

UIViewController+showQuickDialogController.hというカテゴリを作りました。ここでQElementを受けられるようにしておき、そのbindされたデータに基づいてpushかmodalかして次のQuickDialogControllerを表示します。

#import "UIViewController+showQuickDialogController.h"

#import "QuickDialog.h"

@implementation UIViewController (showQuickDialogController)

- (void)pushQuickDialogWithElement:(QElement *)element
{
    id JSON = element.bind;
    [self pushQuickDialogWithJSONDic:JSON];
}

- (void)presentQuickDialogWithElement:(QElement *)element
{
    id JSON = element.bind;
    [self presentQuickDialogWithJSONDic:JSON];
}

- (void)presentQuickDialogWithJSONDic:(NSDictionary *)JSON
{
    [self presentViewController:[self quickDialogControllerWithJSONDic:JSON] animated:YES completion:nil];
}

- (void)pushQuickDialogWithJSONDic:(NSDictionary *)JSON
{
    [self.navigationController pushViewController:[self quickDialogControllerWithJSONDic:JSON] animated:YES];
}

- (QuickDialogController *)quickDialogControllerWithJSONDic:(NSDictionary *)JSON
{
    return [[QuickDialogController alloc]initWithRoot:[[QRootElement alloc]initWithJSON:JSON andData:nil]];
}

@end

使う側では、elementのcontrollerActionにpushQuickDialogWithElement:(or presentQuickDialogWithElement:)を指定して、bindするデータにさらに次の画面用のQRootElementのJSONを入れておく。

#import "UIViewController+showQuickDialogController.h"

// ...省略

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self pushQuickDialogWithJSONDic:[self firstJSON]];
}

- (NSDictionary *)firstJSON
{
    return @{
             @"grouped": @YES,
             @"title": @"First",
             @"sections": @[
                     @{
                         @"elements" : @[
                                 @{
                                     @"type" : @"QLabelElement",
                                     @"imageNamed" : @"aaa",
                                     @"title" : @"First",
                                     @"bind" : [self secondJSON],
                                     @"controllerAction" : @"pushQuickDialogWithElement:"
                                     }
                                 ]
                         }
                     ]
             };
}

- (NSDictionary *)secondJSON
{
    return @{
             @"grouped": @YES,
             @"title": @"Second",
             @"sections": @[
                     @{
                         @"elements" : @[
                                 @{
                                     @"type" : @"QLabelElement",
                                     @"imageNamed" : @"aaa",
                                     @"title" : @"Second"
                                     },
                                 @{
                                     @"type" : @"QLabelElement",
                                     @"imageNamed" : @"aaa",
                                     @"title" : @"Second"
                                     }
                                 ]
                         }
                     ]
             };
}

無事いけました。
secondJSONでもbindとcontrollerActionをfirstJSONと同様につけたらさらにもう一段遷移できます。

f:id:J_ogawa:20140408233516p:plain

f:id:J_ogawa:20140408233525p:plain

本当はbind中のデータからメソッドを作ったりしたかったんですけど、よくわからず、カテゴリでできたのでこれでやっちゃいました。