• iOS:切换视图时,反向传递数据方法一:通知


    通知方式:

         1.有一个(单例)通知中心,负责管理iOS中的所有通知

         2.需要获取某种通知,必须注册成为观察者(订阅)

         3.不再需要取某种通知时,要取消注册。

         4.你可以向通知中心发送某种通知,通知中心会转发给相应的观察者(订阅者)。

    将第一个控制器和第二个控制器以modal(push)方式联接后,每一个控制器和各自的类相关联,同时将segue的idetifier标识设置一个名字,正向传数据时,需要根据segue的标识符进行唯一的识别。反向传数据时,采用通知的方法。

    1、所有的文件:

    2、第一个控制器FirstViewController关联的类为:

    3、第二个控制器SecondViewcontroller关联的类为:

    4、给segue的identifier设置一个名字,作为标识

    具体代码如下:

    FirstViewController控制器关联的viewController(.h/.m)类:

     1 #import "ViewController.h"
     2 #import "SecondViewController.h"
     3 
     4 @interface ViewController ()
     5 @property (weak, nonatomic) IBOutlet UITextField *firstTextField;
     6 
     7 @end
     8 
     9 @implementation ViewController
    10 
    11 - (void)viewDidLoad {
    12     [super viewDidLoad];
    13 }
    14 
    15 //重写该方法,视图切换时,自动调用
    16 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    17 {
    18     if([segue.identifier isEqualToString:@"modal"])
    19     {
    20         //获取目的控制器
    21         SecondViewController *secondVC = segue.destinationViewController;
    22         
    23         //正向传数据
    24         secondVC.information = self.firstTextField.text;
    25         
    26         //注册通知,成为观察者
    27         [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveInfo:) name:NOTIFICATIONTEPY object:nil];
    28     }
    29 }
    30 
    31 //receiveInfo事件
    32 -(void)receiveInfo:(NSNotification*)notification
    33 {
    34     //反向接收通知中的数据
    35     self.firstTextField.text = [notification.userInfo objectForKey:NOTIFICATIONINFOKEY];
    36     
    37     //取消注册
    38     [[NSNotificationCenter defaultCenter]removeObserver:self name:NOTIFICATIONTEPY object:nil];
    39 }
    40 @end

    SecondViewController控制器关联的SecondViewController(.h/.m)类:

     1 #import "SecondViewController.h"
     2 
     3 @interface SecondViewController ()
     4 @property (weak, nonatomic) IBOutlet UITextField *secondTextField;
     5 
     6 @end
     7 
     8 @implementation SecondViewController
     9 //返回时的触发事件
    10 - (IBAction)returnClicked:(UIBarButtonItem *)sender
    11 {
    12     //反向传递数据
    13     
    14     //1、消息内容
    15     NSDictionary *dicInfo = @{NOTIFICATIONINFOKEY:self.secondTextField.text};
    16     
    17     //2、通过通知中心传递出去
    18     [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATIONTEPY object:self userInfo:dicInfo];
    19     
    20     //关闭模态窗口
    21     [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
    22     //[self dismissViewControllerAnimated:YES completion:nil];
    23 }
    24 
    25 - (void)viewDidLoad {
    26     [super viewDidLoad];
    27     
    28     // 显示文本框内容(接受传递过来的数据)
    29     self.secondTextField.text = self.information;
    30 }
    31 
    32 @end
  • 相关阅读:
    springboot对JPA的支持
    springboot整合redis
    spring boot整合mybatis
    mybatis与spring集成
    mybatis动态sql和分页
    mybatis入门
    使用java代码操作redis
    Redis安装和基本操作
    idea安装及使用
    爬虫
  • 原文地址:https://www.cnblogs.com/XYQ-208910/p/4808351.html
Copyright © 2020-2023  润新知