• iOS 多视图—视图切换之代理Delegate传参切换


    步骤:先在iOS平台创建一个工程 ,之后再工程中创建有类(FirstVIewController)
    代码实现如下
    FirstViewController.h
     
    #import <UIKit/UIKit.h>
    //创建协议 声明协议
    @protocol PostVslueDelegate <NSObject>

    -(void)postValue:(NSString *)info;
    @end
     
    @interface FirstViewController : UIViewController<UITextFieldDelegate>

    //声明属性
    @property(strong,nonatomic)UITextField *textName;
    @property(strong,nonatomic)NSString *string;
    @property(strong,nonatomic)id<PostVslueDelegate> Delegate;
    @end
    FirstViewController.m
     

    #import "FirstViewController.h"

    @interface FirstViewController ()

    @end

    @implementation FirstViewController

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

       
    }
    //实现代理方法
    -(BOOL)textFieldShouldReturn:(UITextField *)textField
    {
       //调用代理方法
        if (self.Delegate) {
        [self.Delegate postValue: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,PostVslueDelegate>
    @property(strong,nonatomic)UITextField *textName;
    @property(strong,nonatomic)NSString *string;
    @property(strong,nonatomic)UIButton *myButton;

    @end
     
     
     ViewController.m


    #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
       //背景色
        self.view.backgroundColor=[UIColor greenColor];
        //创建文本框
        self.textName=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 50)];
        self.textName.borderStyle=1;
        //指定代理
        self.textName.delegate=self;
        [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.string=self.textName.text;
        //指定代理
        firstVC.Delegate=self;
       
        //切换视图
        [self presentViewController:firstVC animated:YES completion:^{
          NSLog(@"切换成功");
         
        }];
    }
    //协议方法
    -(void)postValue:(NSString *)info
    {
        //功能:反传参值
        self.textName.text=info;

    }
    //代理方法
    -(BOOL)textFieldShouldReturn:(UITextField *)textField
    {
       //隐藏键盘
        if ([textField isFirstResponder]) {
            [textField resignFirstResponder];
        }
        return YES;
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    @end
     
    效果图
    1、从第一个视图传参(The only)到下一个视图(正向传参)
     
     
    1、从视图传参(The only one of)到上一个视图(逆向传参)
     
     
  • 相关阅读:
    [扩展推荐] Laravel 中利用 GeoIP 获取用户地理位置信息
    10 个优质的 Laravel 扩展推荐
    5 个非常有用的 Laravel Blade 指令,你用过哪些?
    PHP 7.3 我们将迎来灵活的 heredoc 和 nowdoc 句法结构
    使用 Swoole 来加速你的 Laravel 应用
    一个成功的 Git 分支模型(适用于商业应用开发)
    github搜索语法
    python协程爬虫-aiohttp+aiomultiprocess使用
    python-协程、多线程、多进程性能比较
    functools模块-为函数预设args/kwargs参数
  • 原文地址:https://www.cnblogs.com/guiyangxueyuan/p/5280835.html
Copyright © 2020-2023  润新知