6: 2018-01-07 (日) 00:12:01 njf |
7: 2018-01-08 (月) 13:58:57 njf |
| | | |
| *主処理の追加とアプリとしてのテスト [#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で動かそうとするとエラーが起きたときにデバッグがとても大変です。 |
| + | |
| + | *ANE呼び出し処理の追加 [#r244033a] |
| | | |
| &font(Red){作成中}; | | &font(Red){作成中}; |