• 使用block进行界面之间的反向传值


    目标:在两个独立的控制器的界面之间进行反向传值

    关键技术:block

    代码编写及运行环境:Xcode6.4 / 模拟器8.4

    语言:Objective-C

    注:使用纯代码实现,不使用xib/storyboard

    效果图:

    前期注意:

    代码实现如下:

    1.

     1 //
     2 //  AppDelegate.h
     3 //  blockPassValue
     4 //
     5 //  Created by xiaoC on 16/9/28.
     6 //  Copyright (c) 2016年 xiaoLeC. All rights reserved.
     7 //
     8 
     9 #import <UIKit/UIKit.h>
    10 
    11 @interface AppDelegate : UIResponder <UIApplicationDelegate>
    12 
    13 @property (strong, nonatomic) UIWindow *window;
    14 
    15 
    16 @end
     1 //
     2 //  AppDelegate.m
     3 //  blockPassValue
     4 //
     5 //  Created by xiaoC on 16/9/28.
     6 //  Copyright (c) 2016年 xiaoLeC. All rights reserved.
     7 //
     8 
     9 #import "AppDelegate.h"
    10 #import "ViewController.h"
    11 
    12 @interface AppDelegate ()
    13 
    14 @end
    15 
    16 @implementation AppDelegate
    17 
    18 
    19 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    20     // Override point for customization after application launch.
    21     
    22     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    23     self.window.backgroundColor = [UIColor whiteColor];
    24     [self.window makeKeyAndVisible];
    25     
    26     self.window.rootViewController = [[ViewController alloc] init];
    27     
    28     return YES;
    29 }
    30 
    31 - (void)applicationWillResignActive:(UIApplication *)application {
    32     // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    33     // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    34 }
    35 
    36 - (void)applicationDidEnterBackground:(UIApplication *)application {
    37     // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    38     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    39 }
    40 
    41 - (void)applicationWillEnterForeground:(UIApplication *)application {
    42     // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    43 }
    44 
    45 - (void)applicationDidBecomeActive:(UIApplication *)application {
    46     // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    47 }
    48 
    49 - (void)applicationWillTerminate:(UIApplication *)application {
    50     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    51 }
    52 
    53 @end

    2.

     1 //
     2 //  ViewController.h
     3 //  blockPassValue
     4 //
     5 //  Created by xiaoC on 16/9/28.
     6 //  Copyright (c) 2016年 xiaoLeC. All rights reserved.
     7 //
     8 
     9 #import <UIKit/UIKit.h>
    10 
    11 @interface ViewController : UIViewController
    12 
    13 
    14 @end
     1 //
     2 //  ViewController.m
     3 //  blockPassValue
     4 //
     5 //  Created by xiaoC on 16/9/28.
     6 //  Copyright (c) 2016年 xiaoLeC. All rights reserved.
     7 //
     8 
     9 #import "ViewController.h"
    10 #import "YFSecondViewController.h"
    11 
    12 @interface ViewController ()
    13 @property(nonatomic,strong)UILabel *lab;
    14 @end
    15 
    16 @implementation ViewController
    17 
    18 - (void)viewDidLoad {
    19     [super viewDidLoad];
    20     // Do any additional setup after loading the view, typically from a nib.
    21     
    22     self.view.backgroundColor = [UIColor grayColor];
    23     
    24     //添加一个跳转的按钮
    25     UIButton *jumpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    26     jumpBtn.frame = CGRectMake(100, 200, 200, 50);
    27     jumpBtn.backgroundColor = [UIColor yellowColor];
    28     [jumpBtn setTitle:@"跳转" forState:UIControlStateNormal];
    29     [jumpBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    30     [jumpBtn addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
    31     [self.view addSubview:jumpBtn];
    32     
    33     //创建一个label,接收传回来的值
    34     _lab = [[UILabel alloc] initWithFrame:CGRectMake(100, 400, 200, 50)];
    35     _lab.backgroundColor = [UIColor yellowColor];
    36     _lab.textAlignment = NSTextAlignmentCenter;
    37     [self.view addSubview:_lab];
    38 }
    39 
    40 //调用该方法跳转到YFSecondViewController控制器界面
    41 -(void)jump
    42 {
    43     YFSecondViewController *yfSecondV = [[YFSecondViewController alloc] init];
    44     [self presentViewController:yfSecondV animated:YES completion:nil];
    45     
    46     //block代码块
    47     yfSecondV.block = ^void (NSString *str){
    48       
    49         //给label设置返回来的值
    50         _lab.text = str;
    51     };
    52 }
    53 
    54 
    55 
    56 @end

    3.

     1 //
     2 //  YFSecondViewController.h
     3 //  
     4 //
     5 //  Created by xiaoLeC on 16/9/28.
     6 //
     7 //
     8 
     9 #import <UIKit/UIKit.h>
    10 
    11 typedef void(^myBlock)(NSString *str);
    12 
    13 @interface YFSecondViewController : UIViewController
    14 
    15 @property (nonatomic,copy) myBlock block;
    16 
    17 @end
     1 //
     2 //  YFSecondViewController.m
     3 //  
     4 //
     5 //  Created by xiaoLeC on 16/9/28.
     6 //
     7 //
     8 
     9 #import "YFSecondViewController.h"
    10 
    11 @interface YFSecondViewController ()
    12 
    13 @end
    14 
    15 @implementation YFSecondViewController
    16 
    17 - (void)viewDidLoad {
    18     [super viewDidLoad];
    19     self.view.backgroundColor = [UIColor orangeColor];
    20     
    21     //来一些数据
    22     NSArray *arr = @[@"666",@"333",@"我是90后"];
    23     for (int i=0; i<3; i++) {
    24         UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    25         btn.frame = CGRectMake(100, (i*50)+80, 100, 40);
    26         [btn setTitle:arr[i] forState:UIControlStateNormal];
    27         [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    28         [btn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    29         [self.view addSubview:btn];
    30     }
    31     
    32     
    33 }
    34 
    35 -(void)back:(UIButton *)sender
    36 {
    37     //调用block,要在销毁控制器之前调用block
    38     self.block(sender.currentTitle);
    39     
    40     //销毁控制器
    41     [self dismissViewControllerAnimated:YES completion:nil];
    42 }
    43 
    44 - (void)didReceiveMemoryWarning {
    45     [super didReceiveMemoryWarning];
    46     // Dispose of any resources that can be recreated.
    47 }
    48 
    49 
    50 @end

    /*

     block的回调的使用步骤

            1.声明    : 在谁那里调用就在谁那里声明

                实现代码

                    typedef void(^MyBlock)(NSString *name);//block的重命名

                    @property (nonatomic,copy) MyBlock block;//block的声明

            2.实现    : 谁要装值就在谁那里实现

                实现代码

                     SecondViewController *secondVC = [[SecondViewController alloc] init];

                     [self presentViewController:secondVC animated:YES completion:nil];//在这里没用导航控制器,用presentViewController来进入下一个视图

                     //block实现

                     secondVC.block = ^void(NSString *name)

                     {

                        _label.text = name;

                     };//block的位置摆放很作用的,因为它是一个函数,不过一定不能放在使用它的对象的外面和前面就好了

            3.调用    : 谁要传值就在谁那里调用

                    self.block(@"呵呵");//block的调用

     总结一句话:block用在不同视图控制器之间的值回传,回传还有代理、单例,在回传中最简单的就是用block了

     */

  • 相关阅读:
    nno_setup制作升级包必须面临的几个问题 2
    如何运用inno在安装和卸载时提示用户结束进程?
    inno安装卸载时检测程序是否正在运行卸载完成后自动打开网页-代码无效
    Inno Setup 插件大全
    INNO安装卸载自动结束进程插件使用
    Inno Setup 在安装程序开始前和卸载程序开始前,检查并关闭运行的进程
    INNO:检测程序是否已经安装,是则弹出卸载提示。
    Inno Setup 安装、卸载前检测进程或服务
    断点续传2
    解决 IDEA 创建 Gradle 项目没有src目录问题
  • 原文地址:https://www.cnblogs.com/xiao-c-2016/p/5916521.html
Copyright © 2020-2023  润新知