Mais conteúdo relacionado
iOSハンズオントレーニング observer編 (delegate,notification,KVO)
- 7. //
//
//
PictureView.h
!
#import <UIKit/UIKit.h>
!
@interface PictureView : UIImageView
{
@private
NSObject *delegate_;
}
!
@property NSObject *delegate;
!
@end
//
//
//
PictureView.m
!
#import "PictureView.h"
!
@implementation PictureView
@synthesize delegate = delegate_;
!
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.userInteractionEnabled = YES; // UIImageViewはデフォルトでNoなので注意
}
return self;
}
!
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if(delegate_ !=nil) {
[delegate_ pictureSelected];
}
}
!
@end
- 9. //
//
PictureViewDelegate.h (javaでいうところのインターフェース)
//
!
#import <Foundation/Foundation.h>
!
@protocol PictureViewDelegate <NSObject>
@optional
-(void)pictureSelected; // 抽象メソッド
!
@end
//
//
//
PictureView.h
!
#import <UIKit/UIKit.h>
#import "PictureViewDelegate.h"
!
@interface PictureView : UIImageView
{
@private
NSObject <PictureViewDelegate> *delegate_;
}
@property NSObject <PictureViewDelegate> *delegate;
@end
//
//
//
PictureView.m
!
#import "PictureView.h"
!
@implementation PictureView
@synthesize delegate = delegate_;
!
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.userInteractionEnabled = YES;
}
return self;
}
!
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if(delegate_ !=nil) {
if ([delegate_ conformsToProtocol:@protocol(PictureViewDelegate)]) {
if ([delegate_ respondsToSelector:@selector(pictureSelected)]) {
[delegate_ pictureSelected];
}
}
}
}
@end
- 11. //
Screen1ViewController.h
!
#import <UIKit/UIKit.h>
#import "PictureViewDelegate.h"
!
@interface Screen1ViewController : UIViewController <PictureViewDelegate>
!
@end
/
!
Screen1ViewController.m
#import "Screen1ViewController.h"
#import "PictureView.h"
!
@interface Screen1ViewController ()
!
@end
!
@implementation Screen1ViewController
!
- (id)init
{
self = [super init];
if (self) {
// Custom initialization
self.title = @"Album";
self.view.backgroundColor = [UIColor grayColor];
}
return self;
}
!
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage* image01 = [UIImage imageNamed:@"picture01.jpeg"];
PictureView* uiImageView1 = [[PictureView alloc]
initWithFrame:CGRectMake(10, 10, 100, 150)];
uiImageView1.delegate = self;
uiImageView1.image = image01;
[self.view addSubview:uiImageView1];
!
UIImage* image02 = [UIImage imageNamed:@"picture02.jpeg"];
PictureView* uiImageView2 = [[PictureView alloc]
initWithFrame:CGRectMake(120, 10, 100, 150)];
uiImageView2.delegate = self;
uiImageView2.image = image02;
[self.view addSubview:uiImageView2];
}
!
-(void)pictureSelected {
NSLog(@"Push");
}
!
@end
- 14. //
//
//
Screen1ViewController.m
!
#import "Screen1ViewController.h"
#import "PictureView.h"
!
@interface Screen1ViewController ()
!
@end
!
@implementation Screen1ViewController
!
- (id)init
{
self = [super init];
if (self) {
// Custom initialization
self.title = @"Album";
self.view.backgroundColor = [UIColor grayColor];
// NSNotificationCenterを取得する
NSNotificationCenter* center;
center = [NSNotificationCenter defaultCenter];
// Observerとして登録する
[center addObserver:self selector:@selector(pictureSelected:)
name:@"PictureTaped" object:nil];
}
return self;
}
- 16. //
//
//
PictureView.m
!
#import "PictureView.h"
!
@implementation PictureView
!
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.userInteractionEnabled = YES;
}
return self;
}
!
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// NSNotificationを作成する
NSNotification* notification;
notification = [NSNotification notificationWithName:@"PictureTaped"
object:self userInfo:nil];
// NSNotificationCenterを取得する
NSNotificationCenter* center;
center = [NSNotificationCenter defaultCenter];
// 通知を行う
[center postNotification:notification];
}
!
@end
//
//
//
PictureViewDelegate.h
!
#import <Foundation/Foundation.h>
!
@protocol PictureViewDelegate <NSObject>
@optional
-(void)pictureSelected:(NSNotification *)notification;
!
@end
- 18. //
//
//
Screen1ViewController.m
!
#import "Screen1ViewController.h"
#import "PictureView.h"
!
@interface Screen1ViewController ()
!
@end
!
@implementation Screen1ViewController
!
- (id)init
{
self = [super init];
if (self) {
// Custom initialization
self.title = @"Album";
self.view.backgroundColor = [UIColor grayColor];
// NSNotificationCenterを取得する
NSNotificationCenter* center;
center = [NSNotificationCenter defaultCenter];
// Observerとして登録する
[center addObserver:self selector:@selector(pictureSelected:)
name:@"PictureTaped" object:nil];
}
return self;
}
!
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage* image01 = [UIImage imageNamed:@"picture01.jpeg"];
PictureView* uiImageView1 = [[PictureView alloc]
initWithFrame:CGRectMake(10, 10, 100, 150)];
uiImageView1.image = image01;
uiImageView1.tag = 1;
[self.view addSubview:uiImageView1];
!
UIImage* image02 = [UIImage imageNamed:@"picture02.jpeg"];
PictureView* uiImageView2 = [[PictureView alloc]
initWithFrame:CGRectMake(120, 10, 100, 150)];
uiImageView2.image = image02;
uiImageView2.tag = 2;
[self.view addSubview:uiImageView2];
}
!
-(void)pictureSelected:(NSNotification*)notification {
NSLog(@"Push %@",notification.userInfo);
}
!
@end
- 22. //
//
//
Screen1ViewController.m
!
#import "Screen1ViewController.h"
#import "KVOclass.h"
@interface Screen1ViewController ()
!
@end
!
@implementation Screen1ViewController
!
- (id)init
{
self = [super init];
if (self) {
// Custom initialization
self.title = @"KVO Test";
self.view.backgroundColor = [UIColor grayColor];
}
return self;
}
!
- (void)viewDidLoad
{
[super viewDidLoad];
!
!
KVOclass* kvo01 = [KVOclass new];
[kvo01 addObserver:self forKeyPath:@"value" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:nil];
KVOclass* kvo02 = [KVOclass new];
[kvo02 addObserver:self forKeyPath:@"value" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:nil];
kvo01.value = 1; // 値を変更
}
!
- (void)observeValueForKeyPath:(NSString*)keyPath
ofObject:(id)object
change:(NSDictionary*)change
context:(void*)context
{
if ([keyPath isEqual:@"value"])
{
NSLog(@"New Key = %@",[change objectForKey:NSKeyValueChangeNewKey]);
}
}
!
@end