zoukankan      html  css  js  c++  java
  • iOS block在两个页面间的简单传值

    #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 *navigationController = [[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]];
        self.window.rootViewController = navigationController;
        
        
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    @end
    #import <UIKit/UIKit.h>
    
    @interface RootViewController : UIViewController
    
    @end
    #import "RootViewController.h"
    #import "BViewController.h"
    @interface RootViewController ()
    {
        BViewController *bVC;
    }
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
        // 初始化button
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame = CGRectMake(100, 150, 150, 70);
        btn.backgroundColor = [UIColor greenColor];
        [btn setTitle: @"PUSH到下一个控制器" forState:0];
        [btn setTitleColor:[UIColor orangeColor] forState:0];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
        //初始化label
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 70, 150, 70)];
        label.backgroundColor = [UIColor redColor];
        label.textColor = [UIColor yellowColor];
        [self.view addSubview:label];
        //初始化BViewController
        bVC = [[BViewController alloc] init];
        
        // 声明block(注意:这里只是声明,并没有执行block的代码,只有当block被调用才执行下面的代码)
        bVC.titleString = ^(NSString *title){
            label.text = title;
        };
    }
    
    - (void)btnAction:(UIButton *)sender{
        // push到下一个控制器
        [self.navigationController pushViewController:bVC animated:YES];
    }
    
    
    
    @end
    #import <UIKit/UIKit.h>
    
    typedef void(^titleBlock)(NSString *showText);
    
    @interface BViewController : UIViewController
    // 注意:copy 把block从栈区推到堆区
    @property (nonatomic, copy) titleBlock titleString;
    
    @end
    #import "BViewController.h"
    
    @interface BViewController ()
    {
        UITextField *tf;
    }
    @end
    
    @implementation BViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
        // 初始化tf
        tf = [[UITextField alloc] initWithFrame:CGRectMake(80, 100, 150, 30)];
        tf.backgroundColor = [UIColor lightGrayColor];
        tf.text = @"把值传到根控制器";
        tf.textColor = [UIColor yellowColor];
        [self.view addSubview:tf];
    }
    /**
     *  当页面将要消失时,传值到根控制器
     */
    - (void)viewWillDisappear:(BOOL)animated{
        [super viewWillDisappear:animated];
        if (tf.text.length != 0) {
            //执行block的代码(当执行此语句,就会跳到RootViewController的block声明的地方,执行block的代码)
            self.titleString(tf.text);
        }
    }
    
    
    @end
  • 相关阅读:
    博客搬家申请CSDN至博客园
    关于java8新特性的一些总结
    mysql数据库插入时更改数据
    2017总结
    java中单链表的操作
    工厂设计模式及理解
    筛选法求素数
    tcp粘包,断包问题及处理
    理解B树,B+树
    jvm中锁的优化
  • 原文地址:https://www.cnblogs.com/lantu1989/p/5010050.html
Copyright © 2011-2022 走看看