#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
/**
* 通知在一对多的时候使用,从性能考虑一对一时不建议使用
*/
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
//初始化导航控制器
UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]];
self.window.rootViewController = navi;
[self.window makeKeyAndVisible];
return YES;
}
@end
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@end
#import "RootViewController.h"
#import "LFViewController.h"
@interface RootViewController ()
@property(nonatomic, strong) UILabel *messageLabel ;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
//添加按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 150, 60);
[button setTitle:@"push到下一个页面" forState:0];
[button setBackgroundColor:[UIColor greenColor]];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
//添加Label
self.messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 150, 60)];
self.messageLabel.backgroundColor = [UIColor greenColor];
self.messageLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.messageLabel];
[self addNotification];
}
/**
* 添加通知
*/
- (void)addNotification{
/**
* @param receiveNotification: 接收通知后执行的方法
*
* @name: 通知的名字
*/
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"notification" object:nil];
}
/**
* 接收到通知
*/
- (void)receiveNotification:(NSNotification*)info{
NSDictionary *dict = info.userInfo;
NSString *message = dict[@"info"];
if (message) {
NSLog(@"%@",message);
self.messageLabel.text = message;
}
}
/**
* 按钮事件
*/
- (void)buttonAction:(UIButton*)sender{
LFViewController *lfController = [[LFViewController alloc] init];
[self.navigationController pushViewController:lfController animated:YES];
}
-(void)dealloc{
//移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"notification" object:nil];
}
@end
#import <UIKit/UIKit.h>
@interface LFViewController : UIViewController
@end
#import "LFViewController.h"
@interface LFViewController ()
@end
@implementation LFViewController
- (void)viewDidLoad {
[super viewDidLoad];
//添加按钮
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
backBtn.frame = CGRectMake(100, 100, 150, 60);
[backBtn setTitle:@"返回且发送通知" forState:0];
[backBtn setBackgroundColor:[UIColor greenColor]];
[backBtn addTarget:self action:@selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backBtn];
}
/**
* backBtn按钮的事件
*/
- (void)backBtnAction:(UIButton*)sender{
NSDictionary *dict = @{@"info":@"a good news"};
/**
* @Name 通知的名字
* @userInfo 通知携带的信息
*/
NSNotification *notification = [[NSNotification alloc] initWithName:@"notification" object:nil userInfo:dict];
//发送通知
[[NSNotificationCenter defaultCenter] postNotification:notification];
[self.navigationController popViewControllerAnimated:YES];
}
@end