• 属性传值 Block传值


    属性传值 就是将A页面的数据传到B页面上,下面就是将FirstViewController的TextField的内容传递到SecondViewController页面的导航栏标题和控制台输出上

    #import

    @interface FirstViewController :UIViewController

    {

    UITextField *tf;

    }

    @end

    #import "FirstViewController.h"

    #import "SecondViewController.h"//要将数据传到哪一个页面(ViewController)就引用那个头文件

    - (void)viewDidLoad

    {

    [superviewDidLoad];

    //定义一个按钮

    UIButton* button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    button.frame=CGRectMake(100,100,100,100);

    button.backgroundColor= [UIColorredColor];

    [button addTarget:selfaction:@selector(doButton)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:button];

    tf = [[UITextFieldalloc]initWithFrame:CGRectMake(10,300,100,40)];

    tf.tintColor = [UIColororangeColor];

    tf.backgroundColor = [UIColorgrayColor];

    tf.tag =1000;

    [self.viewaddSubview:tf];

    }

    -(void)doButton{

    tf = (UITextField *)[self.viewviewWithTag:1000];

    //push入栈引用计数+1,且控制权归系统

    SecondViewController * seV =[[SecondViewControlleralloc]init];//将其实例化,否则找不到相应的属性

    //直接属性传值

    seV.naviTitle =tf.text;

    seV.str =@"传值成功";//属性(赋值)所要传的值要写在推出下一个窗口前面

    [self.navigationControllerpushViewController:seVanimated:YES];

    }

    @end

    #import

    @interface SecondViewController :UIViewController

    @property(nonatomic,strong)NSString * str;

    @property (nonatomic,retain)NSString *naviTitle;

    @end

    #import "SecondViewController.h"

    @interface SecondViewController ()

    @end

    @implementation SecondViewController

    @synthesize naviTitle =_naviTitle;

    - (void)viewDidLoad

    {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    self.view = [[UIViewalloc]initWithFrame:CGRectMake(0,0,320,480)];

    self.title =self.naviTitle;//只是为了显示第一页面传过来的内容将其显示到导航标题上

    }

    -(void)viewWillAppear:(BOOL)animated{

    NSLog(@"%@",self.str);

    NSLog(@"11---%@",self.naviTitle);

    }

    @end

    Block传值

    #import

    @interface FirstViewController :UIViewController

    {

    UITextField *tf;

    }

    @property (nonatomic,retain)UILabel *label;

    @end

    #import "FirstViewController.h"

    #import "SecondViewController.h"

    @implementation FirstViewController

    - (void)viewDidLoad

    {

    [superviewDidLoad];

    //定义一个按钮

    UIButton* button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    button.frame=CGRectMake(100,100,100,100);

    button.backgroundColor= [UIColorredColor];

    [button addTarget:selfaction:@selector(doButton)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:button];

    //定义一个显示控件

    self.label = [[UILabelalloc]initWithFrame:CGRectMake(0,400, 100, 40)];

    self.label.backgroundColor = [UIColorgreenColor];

    self.label.text = nil;//为了显示第二个视图控制器传过来的字符串

    [self.viewaddSubview:self.label];

    }

    -(void)doButton{

    tf = (UITextField *)[self.viewviewWithTag:1000];

    //push入栈引用计数+1,且控制权归系统

    SecondViewController * seV =[[SecondViewControlleralloc]init];//相对应的将其实例化,否则找不到相应的属性

    //回调方法将输入框中的数据传输过来

    [seVreturnText:^(NSString *showText) {

    self.label.text = showText;

    }];

    [self.navigationControllerpushViewController:seV animated:YES];

    }


    @end

    #import

    typedefvoid (^ReturnTextBlock)(NSString *showText);//重新定义了一个名字

    @interface SecondViewController :UIViewController

    @property (nonatomic,retain)UITextField *tf;

    @property (nonatomic,copy) ReturnTextBlock returnTextBlock;//定义的一个Block属性

    - (void)returnText:(ReturnTextBlock)block;

    @end

    #import "SecondViewController.h"

    - (void)viewDidLoad

    {

    [superviewDidLoad];

    //定义一个输入框 将文字传给第一个界面,并且显示在UILabel上

    self.tf = [[UITextFieldalloc]initWithFrame:CGRectMake(10,300, 100, 40)];

    self.tf.tintColor = [UIColororangeColor];

    self.tf.backgroundColor = [UIColorgrayColor];

    [self.viewaddSubview:self.tf];

    }

    //在第一个界面传进来一个Block语句块的函数

    //把传进来的Block语句块保存到本类的实例变量returnTextBlock(.h中定义的属性)中,然后寻找一个时机调用

    -(void)returnText:(ReturnTextBlock)block{

    self.returnTextBlock = block;

    }

    //而这个时机就是当视图将要消失的时候,需要重写:

    -(void)viewWillDisappear:(BOOL)animated{

    if (self.returnTextBlock !=nil) {

    self.returnTextBlock(self.tf.text);

    NSLog(@"self.tf.text %@",self.tf.text);

    }

    }

    @end

  • 相关阅读:
    leetcode-594-Longest Harmonious Subsequence
    leetcode-581-Shortest Unsorted Continuous Subarray
    leetcode-575-Distribute Candies(计算一个数组中元素的种类的快速方法)
    leetcode-566-Reshape the Matrix
    leetcode-561-Array Partition I
    leetcode-551-Student Attendance Record I(判断是否出现连续几个相同字符)
    Java之CheckedException
    Java异常机制
    Zookeeper分布式协调即分布式锁机制
    Spring中@Value用法
  • 原文地址:https://www.cnblogs.com/dingfuyan/p/5210054.html
Copyright © 2020-2023  润新知