サンプルのコードが悪いのか、それともそもそも根本的に間違っているのか。
とりあえず、サンプルのコードを書き直してみた。
新規プロジェクトをView-based Applicationを選択して名前をMyNotificationとして作成してます。
新規ファイル追加で、NSObjectを継承したMyNotificationを作成しています。
「Cocoa Notification (NSNotification,NSNotificationCenter)」を試しなおす。
説明できることはほとんど前回までと変わらないのでサンプルコードだけ載せる。
- // MyNotificationViewController.h
- #import <UIKit/UIKit.h>
- @interface MyNotificationViewController : UIViewController {
- NSInteger count;
- NSTimer *timer;
- }
- @end
- // MyNotificationViewController.m
- #import "MyNotificationViewController.h"
- #import "MyNotification.h"
- @implementation MyNotificationViewController
- /**
- * 新しくスレッドをたてて、myNotificationのmyWorkメソッドを実行する。
- */
- - (void)newThread
- {
- count++;
- MyNotification *myNotification = [[[MyNotification alloc] init] autorelease];
- [NSThread detachNewThreadSelector:@selector(myWork:) toTarget:myNotification withObject:[NSString stringWithFormat:@"%d",count]];
- if(count > 2) {
- [timer invalidate];
- timer = nil;
- }
- }
- /**
- * 通知受け取ったときに呼ばれる処理
- */
- - (void)owatayo:(id)info
- {
- NSDictionary *userInfo = (NSDictionary *)[info userInfo];
- NSLog(@"メインスレッド:(no.%@スレッドから)通知owataを受け取りowatayoメソッドを実行。", [userInfo objectForKey:@"threadCount"]);
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- NSLog(@"メインスレッド:処理開始");
- // NSNotificationCenterのインスタンスに、
- // addObserver : 通知を受け取るオブジェクト(ここでは自分自身)
- // selector : 通知を受けたときに実行するメソッド
- // name : 通知される通知名
- // object : どのオブジェクトからの通知を受け取るのか指定できる。nilであれば限定しない。
- NSNotificationCenter *center;
- center = [NSNotificationCenter defaultCenter];
- [center addObserver:self selector:@selector(owatayo:) name:@"owata" object:nil];
- NSLog(@"新スレッドを0.5秒間隔で作成します。");
- count = 0;
- timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(newThread) userInfo:nil repeats:YES];
- NSLog(@"メインスレッド:処理終了");
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- }
- - (void)viewDidUnload {
- }
- - (void)dealloc {
- [super dealloc];
- }
- @end
- // MyNotification.h
- #import <Foundation/Foundation.h>
- @interface MyNotification : NSObject {
- }
- - (void)myWork:(NSString *)num;
- - (void)didMyWork:(NSString *)threadCount;
- @end
- // MyNotification.m
- #import "MyNotification.h"
- @implementation MyNotification
- /*
- * 新スレッドでの処理
- */
- - (void)myWork:(NSString *)threadCount
- {
- NSLog(@"(no.%@)新スレッドの処理開始", threadCount);
- NSAutoreleasePool *pool;
- pool = [[NSAutoreleasePool alloc] init];
- [NSThread sleepForTimeInterval:3.0];
- [self didMyWork:threadCount];
- [NSThread sleepForTimeInterval:3.0];
- [pool release];
- NSLog(@"(no.%@)スレッド破棄", threadCount);
- [NSThread exit];
- }
- /*
- * 新スレッド終了するときの通知させる処理
- */
- - (void)didMyWork:(NSString *)threadCount
- {
- NSDictionary *myInfo = [NSDictionary dictionaryWithObjectsAndKeys:threadCount, @"threadCount", nil];
- // 通知する内容を指定
- NSNotification *notification;
- notification = [NSNotification notificationWithName:@"owata" object:self userInfo:myInfo];
- // 通知する(メインスレッドから通知する)
- NSNotificationCenter *center= [NSNotificationCenter defaultCenter];
- [center performSelectorOnMainThread:@selector(postNotification:) withObject:notification waitUntilDone:NO];
- }
- @end
0 件のコメント:
コメントを投稿