yamashiro0110の日記

yamashiro0110の日記です。おもにIT技術のメモを綴っていきます(^o^)

QuickDialogの使い方めも


iOSアプリの開発で設定画面を作成するライブラリを使った

設定画面の作り方は、いくつか参考になるページがあったんだけど ボタンがタップされたときにメソッドを実行する方法とかが、 あまり見当たらなかったのでその辺をφ(`д´)メモメモ...


ライブラリのインストール

CocoaPods使います

  • Podfils
platform :ios, "7.1"
pod 'QuickDialog', '~> 1.0'
pod install

実装

起動時に表示されるViewController

  • TestViewController.h
#import <UIKit/UIKit.h>
#import <QuickDialog/QuickDialog.h>

/**
 * QuickDialogControllerを継承する
 */
@interface TestViewController : QuickDialogController

/**
 *  QuickDialogのRootElementを取得する
 *  @return
 */
+ (QRootElement *)getQRootElement;

@end
  • TestViewController.m
#import "TestViewController.h"

@interface TestViewController ()

@end

@implementation TestViewController

/**
 *  初期化
 *  親クラスのQuickDialogControllerの初期化メソッドをオーバーライド
 */
- (QuickDialogController *)initWithRoot:(QRootElement *)rootElement
{
    if (self = [super initWithRoot:rootElement]) {
        // rootElementのViewControllerを自分自信に設定する
        self.root.controller = self;

        // ボタンのインスタンスを取得
        QElement *button = [self.root elementWithKey:@"login_button"];

        // タップ時に実行されるメソッドを設定する
        button.controllerAction = @"clickButton";
    }

    return self;
}

/**
 *  ボタンがタップされたとき
 */
- (void)clickButton
{
    NSLog(@"ボタンがタップされました");
}

+ (QRootElement *)getQRootElement
{
    // root
    QRootElement *root = [[QRootElement alloc] init];
    root.title = @"設定";
    root.grouped = YES;

    // section
    QSection *section = [[QSection alloc] init];
    section.title = @"Section 1";

    // label
    QLabelElement *label = [[QLabelElement alloc] initWithTitle:@"hello" Value:@"World"];

    // button
    QButtonElement *button = [[QButtonElement alloc] initWithTitle:@"login"];
    [button setKey:@"login_button"];

    [section addElement:label];
    [section addElement:button];
    [root addSection:section];
    return root;
}

@end
  • TestAppDelegate.h
    • 修正なし
#import <UIKit/UIKit.h>

@interface TestAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end
  • TestAppDelegate.m
    • 必要なとこだけ抜粋
#import "TestAppDelegate.h"

// ViewControllerをimport
#import "TestViewController.h"

@interface TestAppDelegate()

@property (nonatomic) TestViewController *viewController;

@end

@implementation TestAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // `QuickDialogController`を継承したViewControllerを初期化
    // 引数の`initWithRoot:`には、`TestViewController`のクラスメソッドである`getQRootElement`の実行結果を渡す
    self.viewController = [[TestViewController alloc] initWithRoot:[TestViewController getQRootElement]];

    // RootViewControllerに設定
    self.window.rootViewController = self.viewController;

    [self.window makeKeyAndVisible];
    return YES;
}

@end

気になったこと

初期化時にボタンのインスタンスを取得して、実行するメソッドを設定したのだけれども プロパティの型はNSStringだったので、どうやって変換するのかがわからなかった

ソースを追ってみると、↓のところぽい

  • QElement.m
- (void)performAction
{
    if (_onSelected!= nil)
        _onSelected();

    if (self.controllerAction!=NULL){
        SEL selector = NSSelectorFromString(self.controllerAction);
        if ([_controller respondsToSelector:selector]) {
            objc_msgSend(_controller,selector, self);
        }  else {
            NSLog(@"No method '%@' was found on controller %@", self.controllerAction, [_controller class]);
        }
    }
}

プロパティのcontrollerActionnilでなければSelectorに変換して、実装されていれば実行するのか。

こんなの出来るって初めて知った・・・^^;


設定画面がこんなに簡単に(^o^)

iOSアプリの開発って、もうOSSライブラリなしじゃツライっす(-_-;)

iOSオープンソースライブラリ徹底活用

iOSオープンソースライブラリ徹底活用