通知模式实现两个textField传值及模态视图——iOS开发

利用通知模式,实现两个不同界面的textField之间的传值,在界面二输入字符,传值到前一界面的textField。

界面的切换,这里临时先用模态视图实现。(重点在传值。所以没纠结设计界面排版。丑了点大家见谅)

大家不要看代码看上去好像挺多。由于我没使用storyboard/xib,是代码实现布局,所以通知和模态视图切换的代码非常少~


实现效果:

点击下一页按钮,进入界面二:

在textField处输入字符串:

点击返回按钮,回到界面一。此时界面一的textField处也有字符串


代码部分:



AppDelegate.m


#import "AppDelegate.h"
#import "ViewController1.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];self.window.backgroundColor = [UIColor whiteColor];[self.window makeKeyAndVisible];ViewController1 *vc1 = [[ViewController1 alloc] init];self.window.rootViewController = vc1;return YES;
}

ViewController1.m


#import "ViewController1.h"
#import "ViewController2.h"@interface ViewController1 ()
{UITextField *text1;
}
@end@implementation ViewController1- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor greenColor];UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 50)];label.text = @"界面一";[self.view addSubview:label];text1 = [[UITextField alloc] initWithFrame:CGRectMake(50, 150, 100, 50)];text1.backgroundColor = [UIColor whiteColor];[self.view addSubview:text1];UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(50, 250, 100, 50)];[button1 addTarget:self action:@selector(pushButtonAction:) forControlEvents:UIControlEventTouchUpInside];[button1 setTitle:@"下一页" forState:UIControlStateNormal];button1.backgroundColor = [UIColor blackColor];[self.view addSubview:button1];//    注冊观察者[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(returnAction:) name:@"chuanzhi" object:nil];// Do any additional setup after loading the view.
}- (void)returnAction:(NSNotification *)text {text1.text = text.userInfo[@"returnInfo"];
}- (void)pushButtonAction:(UIButton *)btn {ViewController2 *vc2 = [[ViewController2 alloc] init];vc2.modalPresentationStyle = UIModalPresentationFullScreen;  //切换效果[self presentViewController:vc2 animated:YES completion:nil];
}

ViewController2.m


#import "ViewController2.h"@interface ViewController2 ()
{UITextField *text2;
}
@end@implementation ViewController2- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor blueColor];UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 50)];label.text = @"界面二";[self.view addSubview:label];text2 = [[UITextField alloc] initWithFrame:CGRectMake(50, 150, 100, 50)];text2.backgroundColor = [UIColor whiteColor];[self.view addSubview:text2];UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(50, 250, 100, 50)];[button2 addTarget:self action:@selector(returnButtonAction:) forControlEvents:UIControlEventTouchUpInside];[button2 setTitle:@"返回" forState:UIControlStateNormal];button2.backgroundColor = [UIColor blackColor];[self.view addSubview:button2];// Do any additional setup after loading the view.
}- (void)returnButtonAction:(UIButton *)btn {NSDictionary *dic = @{@"returnInfo" : self->text2.text};NSNotification *notification = [NSNotification notificationWithName:@"chuangzhi" object:nil userInfo:dic];[[NSNotificationCenter defaultCenter] postNotification:notification];[self dismissViewControllerAnimated:NO completion:nil];
}

通知模式实现两个textField传值及模态视图——iOS开发相关推荐

  1. linux怎么用两个进程传值,linux下的C开发14,可执行程序如何传递参数?模拟shell执行命令...

    上一节介绍了 linux 中的文件类型,并在文章最后使用 C语言编写了程序,该程序能够接受一个文件名参数,并打印出该文件的类型.不知道大家如何,反正我当初学编程时,发现(编译后的)可执行程序居然也能像 ...

  2. Objective-C(十九、通知-消息发送模式之中的一个)——iOS开发基础

    结合之前的学习笔记以及參考<Objective-C编程全解(第三版)>,对Objective-C知识点进行梳理总结. 知识点一直在变.仅仅是作为參考.以苹果官方文档为准~ 十九.通知-消息 ...

  3. 主板模式的两项通用性接口

    主板模式的两项<通用性>接口 by 高煥堂 典型的主板模式(Motherboard pattern)提供了两个通用性接口,也就是:CI和<I>接口.如下图: (图-1) 这两个 ...

  4. mysql数据库的三级模式_数据库的三级模式和两级映射

    数据库的三级模式和两级映射 数据库的三级模式两级映射如下图所示 三级模式 外模式:应用程序用到的部分数据的逻辑结构.一个数据库可以有多个外模式 概念模式(模式.逻辑模式):数据库管理员用到的视图,就是 ...

  5. win10系统过期或处于通知模式

    win10到期和处于通知模式解决方法 此方法不需要产品密钥,目前未发现有什么对电脑影响之处. 链接:https://pan.baidu.com/s/11Q6M5HEVSRdFTkHl8Sg2Wg&am ...

  6. 数据库系统中的三级模式、两级映像以及数据独立性(附绪论思维导图)

    三级模式.两级映像 1.绪论思维导图 2.数据库系统的三级模式结构 2.1.模式 2.2.外模式 2.3.内模式 3.数据库的两级映像功能 3.1.外模式/模式映像 3.2.模式/内模式映像 3.3. ...

  7. FPGA Verilog AD7606驱动代码,包含SPI模式读取和并行模式读取两种

    FPGA Verilog AD7606驱动代码,包含SPI模式读取和并行模式读取两种,代码注释详细 编号:7428665912784264白衫如初oh

  8. 数据库的三级模式和两级映射--简单介绍

    形象解释数据库的三级模式和两级映射 数据库的三级模式和两级映射是美国国家标准协会(American National Standard Institute, ANSI)的数据库管理系统研究小组于197 ...

  9. 数据库系统的三级模式、两级映射

    将数据库结构分为3级:面向用户或应用程序员的用户级.面向建立和维护数据库人员的概念级.面向系统程序员的物理级.而两级映射则连接三级不同模式的通道 假如现在有一个学生信息库. 内模式Internal S ...

最新文章

  1. 语言axff所占字节数_【每日一答】(74)数组名v.s.指针变量,C语言其实很简单形象比喻为“是一个朋友圈的”...
  2. 服务器异常代码413问题
  3. opennms mysql_MySQL用户工具
  4. 怎样通过WireShark抓到的包分析出SIP流程图
  5. Python Matplotlib基本用法
  6. php preg match x,PHP的preg_match噩梦
  7. EPSON 任务同步 改写
  8. 使用Guava对并发应用程序进行基于对象的微锁定
  9. 使用Spring Redis发布/订阅
  10. iphone静态库的加载和调试
  11. 鼠标追踪没用_【擺评】赛睿里最好用的小手鼠标---Rival 3
  12. golang dlv 远程调试
  13. jquery.ajax之beforeSend方法使用介绍
  14. LeetCode 852. Peak Index in a Mountain Array
  15. 有意思的clip-path
  16. 异常值检测算法 IsolationForest、EllipticEnvelope、OneClassSVM实践
  17. Nginx PHP Apache 隐藏版本号/禁止显示版本号
  18. bzero函数_Poll 函数
  19. 基于三点说构成的夹角提取特征点
  20. KNY三人组对YiSmile小程序的项目总结

热门文章

  1. Linux常用命令参考手册02
  2. MYSQL - 存储过程学习笔记
  3. matlab练习程序(图像序列合成视频)
  4. python 类变量和实例变量
  5. openssl evp 对称加密(AES_ecb,ccb)
  6. ionic使用ImagePicker插件中文显示
  7. 基于JMS规范的ActiveMQ
  8. liunx 常用操作
  9. 如何消除选定TextBox后的光标但又不失去焦点。
  10. mediawiki初学心得及使用方法