zoukankan      html  css  js  c++  java
  • UI基础 单例传值 Block 通知

    app.m

    #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]];
        // Override point for customization after application launch.
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        
        
        RootViewController* root=[[RootViewController alloc]init];
        UINavigationController *nav =[[UINavigationController alloc]initWithRootViewController:root];
        self.window.rootViewController=nav;
        
        nav.navigationBar.barTintColor=[UIColor greenColor];
        nav.navigationBar.translucent=NO;
        
        
        
        
        return YES;
    }

    root.h

    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface RootViewController : UIViewController
    
    @end
    
    NS_ASSUME_NONNULL_END

    root.m

    #import "RootViewController.h"
    #import "SecondViewController.h"
    #import "MyData.h"
    @interface RootViewController ()
    
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor=[UIColor yellowColor];
        
        // 跳转
        self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"跳转" style:UIBarButtonItemStylePlain target:self action:@selector(jumpToNext)];
        
        //创建一个单例对象并赋值
        MyData* data =[MyData instance];
        data.name=@"赵日天";
        
        //通知
        //注册通知 (好比打开收音机 调节对应频率)
        //只要调节收音机 就能收到这些广播
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveMessage:) name:@"99.7" object:nil];
        
        
    }
    //收到通知执行的方法 ,参数就是接收到的通知
    -(void)receiveMessage:(NSNotification *)not
    {
        NSLog(@"%@",not.object);
        
    }
    
    //视图要消失
    -(void)viewWillDisappear:(BOOL)animated
    {
        //移除通知 (将收音机关闭)
        [[NSNotificationCenter defaultCenter]removeObserver:self name:@"99.7" object:nil];
        
    }
    
    
    -(void)jumpToNext
    {
        SecondViewController *second =[[SecondViewController alloc]init];
        
        __block RootViewController *tempself=self;
        second.chuanzhiBlock=^(NSString *str){
            NSLog(@"%@",str);
            tempself.navigationItem.title=@"11111";
            
            
            
        };
        
        [self.navigationController pushViewController:second animated:YES];
        
    }
    
    
    @end

    second.h

    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface SecondViewController : UIViewController
    
    @property(nonatomic,copy)void(^chuanzhiBlock)(NSString *str);
    
    
    
    @end
    
    NS_ASSUME_NONNULL_END

    second.m

    #import "SecondViewController.h"
    #import "MyData.h"
    @interface SecondViewController ()
    
    @end
    
    @implementation SecondViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.view.backgroundColor=[UIColor orangeColor];
        
        // 跳转
        self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(jumpBack)];
        
        MyData *data=[MyData instance];
        NSLog(@"%@",data.name);
        
        
        //创建一个对象 并赋值
    }
    
    -(void)jumpBack
    {
        self.chuanzhiBlock(@"block将王尼玛传回去");
        
        //发送广播 (通知)
        [[NSNotificationCenter defaultCenter]postNotificationName:@"99.7" object:@"通知传值"];
        
        [self.navigationController popToRootViewControllerAnimated:YES];
    
        
        
    }
    
    @end

    MyData.h

    #import <Foundation/Foundation.h>
    
    
    
    @interface MyData : NSObject
    
    @property(nonatomic,strong)NSString* name;
    +(MyData *)instance;
    
    @end

    MyData.m

    #import "MyData.h"
    
    @implementation MyData
    
    static MyData *myData =nil;
    +(MyData *)instance
    {
        if (myData ==nil){
            myData =[[MyData alloc] init];
            
        }
        return myData;
        
        
    }
    @end
  • 相关阅读:
    1021. Deepest Root (25)
    1013. Battle Over Cities (25)
    h5ai目录列表优化
    利用chrome调试手机网页
    跨域相关配置
    HttpClient服务端发送http请求
    滚动条样式优化(CSS3自定义滚动条样式 -webkit-scrollbar)
    javaScript复制粘贴
    效率工作
    spring boot实现文件上传下载
  • 原文地址:https://www.cnblogs.com/zhangqing979797/p/13460951.html
Copyright © 2011-2022 走看看