• iOS 多视图—视图切换之代码块传参切换


    在iOS设计中 ,视图在切换的时候同时能传参数到下一个视图页面的方法特别多,这里就以代码块实现传参的方法
     
     
     
    FirstViewController.h

    #import <UIKit/UIKit.h>

    //声明代码块
    typedef void (^PostValueBlock) (NSString *Info);
    @interface FirstViewController : UIViewController<UITextFieldDelegate>


    @property(strong,nonatomic)UITextField *textName;
    @property(strong,nonatomic)NSString *stringB;
    @property(strong,nonatomic)PostValueBlock Block;
    @end
     
    FirstViewController.m

    #import "FirstViewController.h"

    @interface FirstViewController ()

    @end

    @implementation FirstViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
      //设置主屏背景色
        self.view.backgroundColor=[UIColor greenColor];
        //创建文本框
        self.textName=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 40)];
        self.textName.borderStyle=1;
        //指定代理
        self.textName.delegate=self;
        //传值
        self.textName.text=self.stringB;
        [self.view addSubview:self.textName];

       
    }
    //实现代码块方法
    -(BOOL)textFieldShouldReturn:(UITextField *)textField
    {
       //调用代码块方法
        if (self.Block) {
            self.Block(textField.text);
        }
        //切换页面
        [self dismissViewControllerAnimated:YES completion:^{
            NSLog(@"切换成功");
        }];
        //隐藏键盘
        if ([textField isFirstResponder]) {
            [textField resignFirstResponder];
        }
        return YES;
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        
    }
     
     
    @end
     
    ViewController.h
     

    #import <UIKit/UIKit.h>
    #import "FirstViewController.h"
    @interface ViewController : UIViewController<UITextFieldDelegate>
    @property(strong,nonatomic)NSString *stringA;
    @property(strong,nonatomic)UIButton *myButton;
    @property(strong,nonatomic)UITextField *textName;
    @end
    ViewController.m
     

    #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        //背景色
        self.view.backgroundColor=[UIColor blueColor];
        //创建文本框
        self.textName=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 50)];
        self.textName.borderStyle=1;
        //指定代理
        self.textName.delegate=self;
        //传参
        self.textName.text=self.stringA ;//代码块
        [self.view addSubview:self.textName];

        //创建按钮
        self.myButton=[[UIButton alloc]initWithFrame:CGRectMake(150, 160, 50, 50)];
        self.myButton.backgroundColor=[UIColor redColor];
        [self.myButton setTitle:@"Next" forState:UIControlStateNormal];
        [self.myButton addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:self.myButton];
     
    }
    -(void)nextPage
    {
        FirstViewController *firstVC=[[FirstViewController alloc]init];
        //传值到下一页面
       firstVC.stringB=self.textName.text;
        //实现代码块(传值到本页面)
        firstVC.Block=^(NSString *info){
            self.textName.text=info;
        };
        [self presentViewController:firstVC animated:YES completion:^{
           NSLog(@"切换成功");
       
        }];
       
    }
    //协议

    //代理方法
    -(BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        if ([textField isFirstResponder]) {
            [textField resignFirstResponder];
        }
        return YES;
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        
    }
    @end

     
    效果图
    1、从第一个视图传参(The only)到下一个视图(正向传参)
     
     
    1、从视图传参(The only one of)到上一个视图(逆向传参)
     
     
     
  • 相关阅读:
    洛谷 P2058 海港(模拟)
    LA 3708 墓地雕塑(模拟)
    Uva 11300 Spreading the Wealth(贪心)
    UVA 11729 Commando War (贪心)
    【洛谷习题】Likecloud-吃、吃、吃
    【洛谷习题】多米诺骨牌
    【洛谷习题】相似基因
    【NOI1995】石子合并
    【洛谷习题】尼克的任务
    【NOIP2004】合唱队形
  • 原文地址:https://www.cnblogs.com/guiyangxueyuan/p/5280854.html
Copyright © 2020-2023  润新知