iOS编程-------MVC、通知

//
//  AppDelegate.h
//  UI24_MVC通知_
//
//  Created by l on 15/10/8.
//  Copyright (c) 2015年 lon. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end







//
//  AppDelegate.m
//  UI24_MVC通知_
//
//  Created by l on 15/10/8.
//  Copyright (c) 2015年 lon. All rights reserved.
//

#import "AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"


@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.


    //UI24 MVC

    //storyBoard  SB 对象
    UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];

    //first
    FirstViewController *firstVC = [mainSB instantiateViewControllerWithIdentifier:@"first"];

    firstVC.title = @"第一";

    //
    UINavigationController *firstNC = [[UINavigationController alloc] initWithRootViewController:firstVC];


    //second
    SecondViewController *secondVC = [mainSB instantiateViewControllerWithIdentifier:@"second"];
    secondVC.title = @"第二";

    UINavigationController *secondNC = [[UINavigationController alloc] initWithRootViewController:secondVC];


    //third
    ThirdViewController *thirdVC = [mainSB instantiateViewControllerWithIdentifier:@"third"];
    thirdVC.title = @"第三";

    UINavigationController *thirdNC = [[UINavigationController alloc] initWithRootViewController:thirdVC];


    //tabBarController
    UITabBarController *tabBC = [[UITabBarController alloc] init];

    //设置子视图控制器
    tabBC.viewControllers = @[firstNC, secondNC, thirdNC];

    self.window.rootViewController = tabBC;


 return YES;
}

@end














/




//
//  FirstViewController.h
//  UI24_MVC通知_
//
//  Created by l on 15/10/8.
//  Copyright (c) 2015年 lon. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIImageView *imageView;


@end








//
//  FirstViewController.m
//  UI24_MVC通知_
//
//  Created by l on 15/10/8.
//  Copyright (c) 2015年 lon. All rights reserved.
//

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //添加通知观察者
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action:) name:@"notificationName1" object:nil];




    // Do any additional setup after loading the view.
}

- (void)action:(id)boject {

    _imageView.image = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end










/







//
//  SecondViewController.h
//  UI24_MVC通知_
//
//  Created by l on 15/10/8.
//  Copyright (c) 2015年 lon. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@end









//
//  SecondViewController.m
//  UI24_MVC通知_
//
//  Created by l on 15/10/8.
//  Copyright (c) 2015年 lon. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //通知模式
    //观察者模式和通知模式的区别

    //观察者模式:一个对象a 观察对象b 的属性,一旦对象b的属性发生变换,a执行相关的操作.

    //观察者模式的主要特征,观察者和被观察者之间能够直通讯(直到彼此) addObserver removeObserver

    //通知模式 通知模式由通知中心,通知,观察者组成.
    //通知一旦被发布,观察者触发相应发法.
    //观察者和通知之间没有直接联系.

    //通知中心; 通知中心 负责1.发布通知, 2.添加观察者  , 3.移除观察者
    //通知: 负责封装一些发布信息,然后被通知中心发布
    //观察者: 观察某个通知,一旦通知发布,则执行相应的方法

    //通知设计模式步骤
    //1.创建通知中心,添加观察者

    //参数1 观察者
    //参数2 观察者执行的方法
    //参数3 通知名称
    //参数4 通知传递的参数,我们一般置为nil, 默认为nil, 参数会自动传递.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action:) name:@"notificationName1" object:nil];

    //2.发布通知

    //3.触发观察者方法

    //4.移除观察者

    //添加观察者方式2
    //通过方式2 添加的观察者,不需要设置,一旦通知被发布,则执行我们block里面的操作.

    //observer 返回值,为通知的观察者
    __block __weak id observer =
    [[NSNotificationCenter defaultCenter] addObserverForName:@"notificationName2" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {

        //object
        NSLog(@"%@", note.object);

        //通知传递过来的通知信息 userInfo
        NSDictionary *dic = note.userInfo;

        NSLog(@"%@", dic);

        //如果想要只监测一次,移除观察者
        //判断一下 通知是否为空, arc 不用判断.


            [[NSNotificationCenter defaultCenter] removeObserver:observer];

                  }];


    //什么时候使用通知?
    //当我们需要多个页面同时执行一些操作时,使用通知设计模式,发布一个通知,在多个页面添加通知的观察者.



    // Do any additional setup after loading the view.
}


//发布通知
- (IBAction)postNotification:(id)sender {

    //1.创建通知
//    NSNotification *notification = [NSNotification notificationWithName:@"notificationName1" object:@"我是通知参数"];

    //2.发布通知
    //方式1 直接发布通知对象
//    [[NSNotificationCenter defaultCenter] postNotification:notification];

    //方式2 创建通知中心, 发布通知对象,封装了创建通知的过程
    [[NSNotificationCenter defaultCenter] postNotificationName:@"notificationName1" object:@"我是通知参数"];

    //方式3 创建中心,发布通知,传递通知信息userInfo
    [[NSNotificationCenter defaultCenter] postNotificationName:@"notificationName2" object:@"我是通知参数2" userInfo:@{@"key" : @"value"}];




}

//观察者执行的方法
- (void)action:(id)object {

    NSLog(@"%@", object);

    //4. 移除观察者 如果需求是只监测一次通知,那么需要移除观察者
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"notificationName1" object:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end





![这里写图片描述](http://img.blog.csdn.net/20151008145156858)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值