2010年10月19日火曜日

「Cocoa Notification (NSNotification,NSNotificationCenter)」と「キー値監視」(4)

前回の記事も何となく説明の仕方が間違ってるような気がする。
サンプルのコードが悪いのか、それともそもそも根本的に間違っているのか。

とりあえず、サンプルのコードを書き直してみた。

新規プロジェクトをView-based Applicationを選択して名前をMyNotificationとして作成してます。
新規ファイル追加で、NSObjectを継承したMyNotificationを作成しています。

「Cocoa Notification (NSNotification,NSNotificationCenter)」を試しなおす。
説明できることはほとんど前回までと変わらないのでサンプルコードだけ載せる。

  1. //  MyNotificationViewController.h  
  2.   
  3. #import <UIKit/UIKit.h>  
  4.   
  5. @interface MyNotificationViewController : UIViewController {  
  6.  NSInteger count;  
  7.  NSTimer *timer;  
  8. }  
  9.   
  10. @end  

  1. //  MyNotificationViewController.m  
  2.   
  3. #import "MyNotificationViewController.h"  
  4. #import "MyNotification.h"  
  5.   
  6. @implementation MyNotificationViewController  
  7.   
  8. /** 
  9.  * 新しくスレッドをたてて、myNotificationのmyWorkメソッドを実行する。 
  10.  */  
  11. - (void)newThread  
  12. {  
  13.  count++;  
  14.    
  15.  MyNotification *myNotification = [[[MyNotification alloc] init] autorelease];  
  16.  [NSThread detachNewThreadSelector:@selector(myWork:) toTarget:myNotification withObject:[NSString stringWithFormat:@"%d",count]];  
  17.   
  18.  if(count > 2) {  
  19.   [timer invalidate];  
  20.   timer = nil;  
  21.  }  
  22. }  
  23.   
  24. /** 
  25.  * 通知受け取ったときに呼ばれる処理 
  26.  */  
  27. - (void)owatayo:(id)info  
  28. {  
  29.  NSDictionary *userInfo = (NSDictionary *)[info userInfo];  
  30.  NSLog(@"メインスレッド:(no.%@スレッドから)通知owataを受け取りowatayoメソッドを実行。", [userInfo objectForKey:@"threadCount"]);  
  31. }  
  32.   
  33. - (void)viewDidLoad  
  34. {  
  35.     [super viewDidLoad];  
  36.   
  37.  NSLog(@"メインスレッド:処理開始");  
  38.    
  39.  // NSNotificationCenterのインスタンスに、  
  40.  // addObserver : 通知を受け取るオブジェクト(ここでは自分自身)  
  41.  // selector : 通知を受けたときに実行するメソッド  
  42.  // name : 通知される通知名  
  43.  // object : どのオブジェクトからの通知を受け取るのか指定できる。nilであれば限定しない。  
  44.  NSNotificationCenter *center;  
  45.  center = [NSNotificationCenter defaultCenter];  
  46.  [center addObserver:self selector:@selector(owatayo:) name:@"owata" object:nil];  
  47.    
  48.   
  49.  NSLog(@"新スレッドを0.5秒間隔で作成します。");  
  50.  count = 0;  
  51.  timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(newThread) userInfo:nil repeats:YES];  
  52.   
  53.  NSLog(@"メインスレッド:処理終了");  
  54. }  
  55.   
  56. - (void)didReceiveMemoryWarning {  
  57.     [super didReceiveMemoryWarning];  
  58. }  
  59.   
  60. - (void)viewDidUnload {  
  61. }  
  62.   
  63. - (void)dealloc {  
  64.     [super dealloc];  
  65. }  
  66.   
  67. @end  

  1. //  MyNotification.h  
  2.   
  3. #import <Foundation/Foundation.h>  
  4.   
  5. @interface MyNotification : NSObject {  
  6. }  
  7.   
  8. - (void)myWork:(NSString *)num;  
  9. - (void)didMyWork:(NSString *)threadCount;  
  10.   
  11. @end  

  1. //  MyNotification.m  
  2.   
  3. #import "MyNotification.h"  
  4.   
  5. @implementation MyNotification  
  6.   
  7. /* 
  8.  * 新スレッドでの処理 
  9.  */  
  10. - (void)myWork:(NSString *)threadCount  
  11. {  
  12.  NSLog(@"(no.%@)新スレッドの処理開始", threadCount);  
  13.   
  14.  NSAutoreleasePool *pool;  
  15.  pool = [[NSAutoreleasePool alloc] init];  
  16.  [NSThread sleepForTimeInterval:3.0];  
  17.  [self didMyWork:threadCount];  
  18.  [NSThread sleepForTimeInterval:3.0];  
  19.  [pool release];  
  20.   
  21.  NSLog(@"(no.%@)スレッド破棄", threadCount);  
  22.  [NSThread exit];  
  23. }  
  24.   
  25. /* 
  26.  * 新スレッド終了するときの通知させる処理 
  27.  */  
  28. - (void)didMyWork:(NSString *)threadCount  
  29. {  
  30.  NSDictionary *myInfo = [NSDictionary dictionaryWithObjectsAndKeys:threadCount, @"threadCount", nil];  
  31.   
  32.  // 通知する内容を指定  
  33.  NSNotification *notification;  
  34.  notification = [NSNotification notificationWithName:@"owata" object:self userInfo:myInfo];   
  35.   
  36.  // 通知する(メインスレッドから通知する)  
  37.  NSNotificationCenter *center= [NSNotificationCenter defaultCenter];  
  38.  [center performSelectorOnMainThread:@selector(postNotification:) withObject:notification waitUntilDone:NO];  
  39. }  
  40.   
  41. @end  

0 件のコメント:

コメントを投稿