1.关于NSNotification 的使用这个类实际就是一个通知中心,该对象有两个重要的成员变量:name 和object,还有一个用来传递更多信息的userInfo
/**************** Notifications ****************/
@interface NSNotification :NSObject <NSCopying, NSCoding>
- (NSString *)name;
- (id)object;
- (NSDictionary *)userInfo;
@end
该类有两个两种方法来传递信息:
+ (id)notificationWithName:(NSString *)aName object:(id)anObject;
+ (id)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;
用第二个类方法可以传递更多的信息:
传递
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize button;
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(reserve_notification:)name:@"Meth"object:nil];
button=[[UIButtonalloc] initWithFrame:CGRectMake(20,60,100,35)];
button.backgroundColor=[UIColorblueColor];
self.view.backgroundColor=[UIColorwhiteColor];
[buttonsetTitle:@"Changed Color"forState:UIControlStateNormal];
[buttonsetTitleColor:[UIColorblackColor] forState:UIControlStateHighlighted];
[buttonsetTitleShadowColor:[UIColorblueColor] forState:UIControlStateHighlighted];
[buttonaddTarget:selfaction:@selector(sendNotification)forControlEvents:UIControlEventTouchDown];
[self.view addSubview:button];
}
-(void)sendNotification
{
NSLog(@"sendNotfication");
NSDictionary *userInfo=[NSDictionarydictionaryWithObjectsAndKeys:@"user1",@"key1",@"user2",@"key2",nil];
[[NSNotificationCenterdefaultCenter] postNotificationName:@"Meth"object:niluserInfo:userInfo];
}
-(void)reserve_notification:(NSNotification *)notify
{
NSLog(@"userInfo descrption %@",[userInfodescription]);
self.view.backgroundColor=[UIColorredColor];
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end