ページへ戻る
− Links
印刷
iOSでのANEの作成/iOS側の作成
の編集 ::
NJF Wiki
xpwiki
:
iOSでのANEの作成
/
iOS側の作成
の編集
# n6d6c2ec の編集
ページ内容:
*主処理の追加とアプリとしてのテスト [#n6d6c2ec] ライブラリの中心となるアラート表示の部分と、テスト用にそれを呼び出すボタンの処理を作成します。 まず、アラートを表示する「anealert.m」の処理は以下の通りです。 #import "anealert.h" @interface AneAlert() - (void)onYesButtonPressed; @end @implementation AneAlert - (void) alertWindowInit{ if(alertController == nil){ NSLog(@"alertWindowInit"); alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert]; // Yes button [alertController addAction:[UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [self onYesButtonPressed]; }]]; } } - (void) alertWindowShow:(NSString*)title : (NSString*)message{ NSLog(@"alertWindowShow"); id delegate = [[UIApplication sharedApplication] delegate]; UIWindow * win = [delegate window]; alertController.title = title; alertController.message = message; [win.rootViewController presentViewController:alertController animated:YES completion:nil]; } - (void)onYesButtonPressed { NSLog(@"onOtherButtonPressed"); } @end これに対応するanealert.hは以下の通りです。 #import <UIKit/UIKit.h> @interface AneAlert : NSObject{ @private UIAlertController *alertController; UIViewController *viewController; } - (void) alertWindowInit; - (void) alertWindowShow:(NSString*)title : (NSString*)message; @end 基本的に通常のObjective-Cでのアラート表示の処理ですが、viewControllerの取得方法には注意が必要です。 つい、AneAlertSampleプロジェクトの中のViewControllerを使いたくなりますが、これはテスト用のクラスであり、ANEとして実行するときには存在しません。そのため、少し面倒でも、上記alertWindowShowメソッド中で行っているように、UIApplication sharedApplicationからUIWindowを参照し、そこからrootViewControllerを引き出す必要があります。 この方法は画面表示を伴うANEの作成では必須と言えるほどよく使うので、おぼえておいた方が良いでしょう。 これを呼び出すViewController.mの処理は、前述のボタンの処理を以下のように書きかえます。 - (IBAction)alertShowUp:(id)sender { NSLog(@"alertShowUp"); if(aneAlert == nil){ aneAlert = [[AneAlert alloc] init]; [aneAlert alertWindowInit]; cnt = 1; } NSString *message = [NSString stringWithFormat:@"%d times.", cnt]; [aneAlert alertWindowShow:@"Ane Test from objective-C" : message]; cnt++; } また、ここで使う変数の定義も行います。 #import "ViewController.h" #import "anealert.h" @interface ViewController (){ AneAlert *aneAlert; int cnt; } @end これで実行すると、ボタンを押すたびにメッセ時の数字が変わるアラートが表示されます。 &ref(sciOS.jpg,mw:480,mh:360); このように、ANEの作成では一度テスト用のアプリを作って実行してみるのがお勧めです。 そうではなく、いきなりANEとして作成してAirで動かそうとするとエラーが起きたときにデバッグがとても大変です。
編集の要約:
Q & A 認証:
ページ更新時は次の質問にお答えください。(プレビュー時は必要ありません)
Q:
「東京」の読みがな?(ひらがなで)
A:
お名前:
タイムスタンプを変更しない
テキスト整形のルールを表示する
[1]
Links list
(This host) = https://njf.jp
(This host)
/cms/modules/xpwiki/?cmd=edit&help=true&page=iOS%E3%81%A7%E3%81%AEANE%E3%81%AE%E4%BD%9C%E6%88%90%2FiOS%E5%81%B4%E3%81%AE%E4%BD%9C%E6%88%90