zoukankan      html  css  js  c++  java
  • 界⾯间传值

    界⾯间传值,分两种,⼀种情况是push⼊栈,从上⼀级视图到下⼀级视图,这只需要在下⼀级视图中,定义⼀个属性,⽤来把上⼀级的视图的值接收,在进⼊下⼀集视图之前封装好,然后在就可以在本视图中得到上⼀级传下来的值。 
    另⼀种是pop出栈,逆向传值,这样的情况,由于 pop出栈的视图数据全部销毁,所以不能通过属性直接传值,此时可以⽤协议,协议⾥定义⼀个可以传值的⽅法,参数即为要传出去的值。然后再在本视图中声明⼀个属性,遵循这个协议。这个视图就是相当于雇主,设置了⼀个协议,需要⼀个代理来实现这个协议的⽅法。此时就让上⼀级视图来接收这个协议,实现协议中的⽅法,这个时候,在下⼀级视图即将被 pop出栈之前,⽤⾃⼰的代理来调⽤这个协议⾥的⽅法,此时的上⼀级的视图实现的⽅法的参数就可以赋给上⼀级视图的接受对象。
    第一种(点击到下一级视图)
     
    //
    //  RootViewController.m
    //  界面传值
    //
    //  Created by cqy on 16/2/16.
    //  Copyright © 2016年 程清杨. All rights reserved.
     
    #import "RootViewController.h"
    #import "FirstViewController.h"
    @interface RootViewController (){
        UITextField *Text;
    }

    @end

    @implementation RootViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self p_setupView];
       
        self.title = @"界面传值";
        self.navigationController.navigationBar.barTintColor = [UIColor redColor];
        UIButton *RightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        RightBtn.frame = CGRectMake(0, 0, 40, 30);
        [RightBtn setTitle:@"跳转" forState:UIControlStateNormal];
        [RightBtn addTarget:self action:@selector(RightBtnaction:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *Rightitem = [[UIBarButtonItem alloc]initWithCustomView:RightBtn];
        self.navigationItem.rightBarButtonItem = Rightitem;
       
       
        // Do any additional setup after loading the view.
    }
    -(void)p_setupView{
        self.view.backgroundColor = [UIColor yellowColor];
        //创建 UITextField
        Text = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 200, 50)];
        Text.borderStyle = UITextBorderStyleRoundedRect;
        [self.view addSubview:Text];
       
    }
    -(void)RightBtnaction:(UIButton *)sender{
        FirstViewController *firstView = [[FirstViewController alloc]init];
        // 使⽤属性接收值在下⼀级中声明的属性testStr就是⽤来接收上⼀级传过去值的对象。到这⾥就可获取到上个视图传到下⼀个视图的值。
        firstView.textStr = Text.text;
        [self.navigationController pushViewController:firstView animated:YES];
    }
    #pragma mark 响应触摸事件
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        // 释放第⼀响应者,⼿键盘
       [Text resignFirstResponder];
        // 阻断响应者链,也可以⼿键盘
        //self.view.userInteractionEnabled = NO;
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    /*
    #pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */
    @end
    第二种情况逆向传值
    //
    //  PassValueDelegate.h
    //  界面传值
    //
    //  Created by cqy on 16/2/16.
    //  Copyright © 2016年 程清杨. All rights reserved.
    //
    #import <Foundation/Foundation.h>

    @protocol PassValueDelegate <NSObject>
    - (void)passValue:(NSString *)aString;
    @end
     
    //
    //  FirstViewController.h
    //  界面传值
    //
    //  Created by cqy on 16/2/16.
    //  Copyright © 2016年 程清杨. All rights reserved.
    //

    #import <UIKit/UIKit.h>
    #import "PassValueDelegate.h"
    @interface FirstViewController : UIViewController
    // ⽤来接收上⼀个⻚⾯的值
    @property(nonatomic,copy)NSString *textStr;
    // 声明代理
    @property(nonatomic,assign)id<PassValueDelegate>delegate;
    @end
     
    //
    //  FirstViewController.m
    //  界面传值
    //
    //  Created by cqy on 16/2/16.
    //  Copyright © 2016年 程清杨. All rights reserved.
    //

    #import "FirstViewController.h"

    @interface FirstViewController ()
    {
        UITextField *Textfield;
    }
    @end

    @implementation FirstViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
        NSLog(@"----%@",_textStr);
        Textfield = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 200, 50)];
        Textfield.borderStyle = UITextBorderStyleRoundedRect;
        [self.view addSubview:Textfield];
       
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemReply) target:self action:@selector(leftButtonAction:)];
       
       
       
        // Do any additional setup after loading the view.
    }
    - (void)leftButtonAction:(UIBarButtonItem *)sender{
        //在pop出栈之前,⽤代理调协议的⽅法参数就是要传过去的值。
        // 使⽤代理⽅法
        [self.delegate passValue:Textfield.text];
        [self.navigationController popViewControllerAnimated:YES];
    }
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        // 释放第⼀响应者,⼿键盘
        [Textfield resignFirstResponder];
        // 阻断响应者链,也可以⼿键盘
        //self.view.userInteractionEnabled = NO;
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    /*
    #pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */
    @end
     
    //
    //  RootViewController.h
    //  界面传值
    //
    //  Created by cqy on 16/2/16.
    //  Copyright © 2016年 程清杨. All rights reserved.
    //

    #import <UIKit/UIKit.h>
    #import "PassValueDelegate.h"
    //上⼀级视图(RootViewController)接收协议,遵循协议。
    @interface RootViewController : UIViewController<PassValueDelegate>
    @end
     
    //
    //  RootViewController.m
    //  界面传值
    //
    //  Created by cqy on 16/2/16.
    //  Copyright © 2016年 程清杨. All rights reserved.
    //

    #import "RootViewController.h"
    #import "FirstViewController.h"
    @interface RootViewController (){
        UITextField *Text;
    }

    @end

    @implementation RootViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self p_setupView];
       
        self.title = @"界面传值";
        self.navigationController.navigationBar.barTintColor = [UIColor redColor];
        UIButton *RightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        RightBtn.frame = CGRectMake(0, 0, 40, 30);
        [RightBtn setTitle:@"跳转" forState:UIControlStateNormal];
        [RightBtn addTarget:self action:@selector(RightBtnaction:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *Rightitem = [[UIBarButtonItem alloc]initWithCustomView:RightBtn];
        self.navigationItem.rightBarButtonItem = Rightitem;
       
     
        // Do any additional setup after loading the view.
    }
    -(void)passValue:(NSString *)aString{
       NSLog(@"--%@",aString);
        Text.text = aString;
    }
    -(void)p_setupView{
        self.view.backgroundColor = [UIColor yellowColor];
        //创建 UITextField
        Text = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 200, 50)];
        Text.borderStyle = UITextBorderStyleRoundedRect;
        [self.view addSubview:Text];
       
    }
    -(void)RightBtnaction:(UIButton *)sender{
        FirstViewController *firstView = [[FirstViewController alloc]init];
        // 使⽤属性接收值在下⼀级中声明的属性testStr就是⽤来接收上⼀级传过去值的对象。到这⾥就可获取到上个视图传到下⼀个视图的值。
        firstView.textStr = Text.text;
        [self.navigationController pushViewController:firstView animated:YES];
        firstView.delegate = self;
    }
    #pragma mark 响应触摸事件
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        // 释放第⼀响应者,⼿键盘
       [Text resignFirstResponder];
        // 阻断响应者链,也可以⼿键盘
        //self.view.userInteractionEnabled = NO;
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    /*
    #pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */
    @end
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    解决"从不是创建控件的线程访问它"
    (转)Asp.net 中 Get和Post 的用法
    正则表达式30分钟入门教程 v2.3 转
    winform中,如何控制控件位置随窗体的大小改变而改变
    SpringCloudNetflix(Usage of Hystrix)
    SpringCloudNetflix(Usage of Eureka)
    分布式协调Zookeeper(分布式锁&Leader选举)
    SpringCloudNetflix(Usage of GateWay)
    分布式协调Zookeeper(手写配置中心&动态刷新)
    SpringCloudNetflix(Usage of Nacos配置中心)
  • 原文地址:https://www.cnblogs.com/iQingYang/p/5193225.html
Copyright © 2011-2022 走看看