六种传值方式之非代理传值

作者: fuxi | 来源:发表于2016-08-21 12:24 被阅读0次

用非代理的方式在A、B两个页面之间实现值的传递

思路如下:

在A页面声明一个公有属性Label,然后在B页面使用presentingViewController方法去获取弹窗消失后的视图,将B页面上的TextField的内容传给Label。

完整代码:
A页面
.h文件
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@property (nonatomic, strong) UILabel *label;
@end

.m文件
#import "RootViewController.h"
#import "ModalViewController.h"

@interface RootViewController ()
@end

@implementation RootViewController
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    //1) 设置背景颜色 
    self.view.backgroundColor = [UIColor cyanColor]; 
    //2) 设置一个Label 
    //a) 创建一个Label 
    _label = [[UILabel alloc] initWithFrame:CGRectMake(25, 100, 270, 30)]; 
    //b) 设置该label的tag 
    _label.tag = 1000; 
    //c) 设置label的内容 
    _label.text = @"非代理传值"; 
    //d) 设置背景颜色
     _label.backgroundColor = [UIColor orangeColor]; 
    //e) 设置字体颜色 
    _label.textColor = [UIColor whiteColor];         
    //f) 设置居中方式
     _label.textAlignment = NSTextAlignmentCenter; 
    //g) 添加label 
    [self.view addSubview:_label]; 
    //3) 设置跳转你的button 
    //a) 创建button 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; 
    //b) 设置其frame 
    button.frame = CGRectMake(0, 0, 200, 30); 
    //c) 设置其在屏幕的中心 
    button.center = self.view.center; 
    //d) 设置背景颜色 
    button.backgroundColor = [UIColor lightGrayColor]; 
    //e) 设置显示的内容 
    [button setTitle:@"跳转" forState:UIControlStateNormal]; 
    //f) 设置相应事件 
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; 
    //g) 添加到页面上 
    [self.view addSubview:button];}

    #pragma mark - 点击的按键响应
    - (void) buttonAction: (UIButton *) button {
           //1. 初始化 
          ModalViewController *modalViewController = [[ModalViewController alloc] init]; 
            //2. 弹窗模式 
          modalViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
            //3. 模态视图 
          [self presentViewController:modalViewController animated:YES completion:nil];
      
    }
    @end

B页面
.h文件
#import <UIKit/UIKit.h>
@interface ModalViewController : UIViewController
@end
.m文件
#import "ModalViewController.h"
#import "RootViewController.h"
@interface ModalViewController ()
@end

    @implementation ModalViewController        
    - (void)viewDidLoad { 
            [super viewDidLoad]; 
            //1) 设置背景颜色 
            self.view.backgroundColor = [UIColor orangeColor]; 
            //2) 设置一个TextField 
            //a) 创建一个TextField 
            UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(25, 100, 270, 30)]; 
            //b) 设置tag textField.tag = 2000; 
            //c) 设置显示的效果 
            textField.borderStyle = UITextBorderStyleRoundedRect; 
            //d) 显示提示语 
            textField.placeholder = @"请输入一段文字..."; 
            //e) 添加到self.view上
             [self.view addSubview:textField];         
            //3) 设置跳转你的button 
            //a) 创建button 
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; 
            //b) 设置其frame 
            button.frame = CGRectMake(0, 0, 200, 30);
             //c) 设置其在屏幕的中心 
            button.center = self.view.center; 
            //d) 设置背景颜色 
            button.backgroundColor = [UIColor lightGrayColor]; 
            //e) 设置显示的内容 
            [button setTitle:@"返 回" forState:UIControlStateNormal];
             //f) 设置相应事件 
            [button addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside]; 
            //g) 添加到页面上 
            [self.view addSubview:button];}

    - (void) backAction: (UIButton *) button { 
            //1) 取值
             UITextField *textField = (UITextField *)[self.view viewWithTag:2000];
             //2) 修改值 
            //说明:presentingViewController这个方法是获取到谁弹出了该视图控制器,也就是获取到弹窗消失后的视图 
            RootViewController *presentingVC = (RootViewController *)self.presentingViewController; 
            presentingVC.label.text = textField.text; 
            //3) 关闭模态视图
             [self dismissViewControllerAnimated:YES completion:nil];
    }
    @end

相关文章

  • 六种传值方式之非代理传值

    用非代理的方式在A、B两个页面之间实现值的传递 思路如下: 完整代码:A页面.h文件#import @inter...

  • Vue组件间关系及六种传值方式

    前言: 六种传值方式为: 属性传值 $refs $parent 通知传值(广播传值) 本地传值 路由传值 在介绍组...

  • iOS-传值方式

    传值方式:1、属性传值 方法传值2、代理传值3、单例传值 4、通知传值 NSNotificationCente...

  • iOS中界面传值的几种方式

    1.属性传值 属性传值适用于顺序传值,从前面的界面传值给后面的界面。 2.代理传值 用代理的方式实现界面间传值稍微...

  • 页面传值-03

    一、传值分类 页面传值基本分为两种:正向传值和反向传值。 二、传值方式 传值,最基本的无非就是代理传值、通知传值、...

  • iOS 页面(代理、通知、block、单例、属性)传值

    一、传值分类 页面传值基本分为两种:正向传值和反向传值。 二、传值方式 传值,最基本的无非就是代理传值、通知传值、...

  • iOS六种传值方式之代理模式

    实现方式有六种,分别是:代理传值、观察者模式(KVO)、通知、单例模式、block以及非代理。将思路一一总结如下:...

  • iOS传值方式

    在iOS中,常见的传值方式有以下几种:1.属性传值2.单例传值3.通知传值4.代理传值5.Block这些传值方式,...

  • iOS 传值方法(属性传值、代理传值、Block、通知、单例)

    iOS 传值方法(属性传值、代理传值、Block、通知、单例)简单的介绍一下几个传值方式 1、属性传值 在传值的时...

  • iOS 常用传值方式

    总结 iOS 日常开发中的几种常用传值方式:正向传值代理传值block传值通知传值单例 文章代码:https://...

网友评论

    本文标题:六种传值方式之非代理传值

    本文链接:https://www.haomeiwen.com/subject/hmtwsttx.html