建4个视图 前一个能推出后一个 最后一个返回第一个并且第一个页面背景变成黑色
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
#import "MainViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "ForthViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
MainViewController *mainVC = [[MainViewController alloc]init];
//导航控制器的使用
UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:mainVC];
//把导航控制器指定为window的根视图控制器
self.window.rootViewController = navi;
[mainVC release];
[navi release];
return YES;
}
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController
@end
#import "MainViewController.h"
#import "SecondViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (void)dealloc
{
#warning 通知中心的使用 : 3. 在注册观察事件的对象被销毁的时候,要在dealloc方法中从通知中心移除
[[NSNotificationCenter defaultCenter]removeObserver:self];
// [self.view removeObserver:self forKeyPath:@"tag"];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"第一页";
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(20, 60, 335, 40);
button.backgroundColor = [UIColor yellowColor];
[button setTitle:@"button" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
//点击第四个界面第一个界面变色 干活的是第一个界面
//1.注册观察着 要监听的事件
//2. 发送通知 在将来某一时刻根据事件发送通知
#warning 通知中心的使用: 1. 在通知中心注册一个观察事件
//通知中心(单例方法)
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
//注册一个观察事件
//参数1 : target
//参数2 : action
//参数3 : 时间的标识
//参数4 : 限定发送通知的对象
[center addObserver:self selector:@selector(receiveNotification:) name:@"changeColor" object:nil];
//#warning KVO的使用 1.
// //key-value-observer 键值观察
// // 1. 添加一个观察者
// //由被观察者调用
// //参数1 : 观察对象(最后干活的人)
// //参数2 : 被观察者的成员变量的名字(属性名)
// //参数3 : 在观察者方法中,保存的是新值还是旧值
// //参数4 : 任意指针类型 向观察者方法中传递值
// [self.view addObserver:self forKeyPath:@"tag" options:NSKeyValueObservingOptionNew context:@"测试"];
//
// self.view.tag = 1000;
}
//#warning KVO的使用 2.
KVO 要实现的观察者方法,固定的
//- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
//{
// NSLog(@"%@",keyPath);
// NSLog(@"%@",object);
// NSLog(@"%@",change);
// NSLog(@"%@",context);
//}
//收到通知后执行的方法
- (void)receiveNotification:(NSNotification *)noti
{
// self.view.backgroundColor = [UIColor blackColor];
NSLog(@"%@",noti.userInfo);
self.view.backgroundColor = noti.object;
}
- (void)buttonAction:(UIButton *)btn
{
NSLog(@"点击");
SecondViewController *secondVC = [[SecondViewController alloc]init];
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
}
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@end
#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)dealloc
{
[self.view removeObserver:self forKeyPath:@"tag"];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"第二页";
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(20, 60, 350, 40);
button.backgroundColor = [UIColor yellowColor];
[button setTitle:@"button" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
#warning KVO的使用 1.
//key-value-observer 键值观察
// 1. 添加一个观察者
//由被观察者调用
//参数1 : 观察对象(最后干活的人)
//参数2 : 被观察者的成员变量的名字(属性名)
//参数3 : 在观察者方法中,保存的是新值还是旧值
//参数4 : 任意指针类型 向观察者方法中传递值
[self.view addObserver:self forKeyPath:@"tag" options:NSKeyValueObservingOptionNew context:@"测试"];
self.view.tag = 1000;
}
#warning KVO的使用 2.
//KVO 要实现的观察者方法,固定的
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"%@",keyPath);
NSLog(@"%@",object);
NSLog(@"%@",change);
NSLog(@"%@",context);
}
- (void)buttonAction:(UIButton *)btn
{
NSLog(@"press");
ThirdViewController *thirdVC = [[ThirdViewController alloc]init];
[self.navigationController pushViewController:thirdVC animated:YES];
[thirdVC release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#import <UIKit/UIKit.h>
@interface ThirdViewController : UIViewController
@end
#import "ThirdViewController.h"
#import "ForthViewController.h"
@interface ThirdViewController ()
@end
@implementation ThirdViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"第三页";
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(20, 60, 350, 40);
button.backgroundColor = [UIColor yellowColor];
[button setTitle:@"button" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonAction:(UIButton *)btn
{
NSLog(@"press");
ForthViewController *forthVC = [[ForthViewController alloc]init];
[self.navigationController pushViewController:forthVC animated:YES];
[forthVC release];
}
#import <UIKit/UIKit.h>
@interface ForthViewController : UIViewController
@end
#import "ForthViewController.h"
#import "MainViewController.h"
@interface ForthViewController ()
@end
@implementation ForthViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"第四页";
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(20, 60, 350, 40);
button.backgroundColor = [UIColor yellowColor];
[button setTitle:@"button" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonAction:(UIButton *)btn
{
NSLog(@"press");
[self.navigationController popToRootViewControllerAnimated:YES];
#warning 通知中心的使用 : 2. 在某个地方发送通知
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
//发送通知
//参数1 : 前面填啥后边填啥 事件标识
//参数2 : 向接受通知的对象传一个值(对象)
//参数3 : 系统使用这个参数发送一些值
[center postNotificationName:@"changeColor" object:[UIColor blackColor] userInfo:@{@"key":@"value"}];
}