zoukankan      html  css  js  c++  java
  • 通知传值

    #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
  • 相关阅读:
    laravel队列
    php程序内存优化之数组操作优化
    swoole4创建Mysql连接池
    MySQL创建索引
    mysql索引命中规则
    Redis数据类型及使用场景
    Redis高可用集群-哨兵模式(Redis-Sentinel)
    网站架构优化性能
    PHP实现Redis分布式锁
    微软公司面试题
  • 原文地址:https://www.cnblogs.com/lantu1989/p/5421494.html
Copyright © 2011-2022 走看看