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

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

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

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


实现效果:

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

在textField处输入字符串:

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


代码部分:



AppDelegate.m


  1. #import "AppDelegate.h"
  2. #import "ViewController1.h"
  3. @interface AppDelegate ()
  4. @end
  5. @implementation AppDelegate
  6. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  7. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  8. self.window.backgroundColor = [UIColor whiteColor];
  9. [self.window makeKeyAndVisible];
  10. ViewController1 *vc1 = [[ViewController1 alloc] init];
  11. self.window.rootViewController = vc1;
  12. return YES;
  13. }

ViewController1.m


  1. #import "ViewController1.h"
  2. #import "ViewController2.h"
  3. @interface ViewController1 ()
  4. {
  5. UITextField *text1;
  6. }
  7. @end
  8. @implementation ViewController1
  9. - (void)viewDidLoad {
  10. [super viewDidLoad];
  11. self.view.backgroundColor = [UIColor greenColor];
  12. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 50)];
  13. label.text = @"界面一";
  14. [self.view addSubview:label];
  15. text1 = [[UITextField alloc] initWithFrame:CGRectMake(50, 150, 100, 50)];
  16. text1.backgroundColor = [UIColor whiteColor];
  17. [self.view addSubview:text1];
  18. UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(50, 250, 100, 50)];
  19. [button1 addTarget:self action:@selector(pushButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  20. [button1 setTitle:@"下一页" forState:UIControlStateNormal];
  21. button1.backgroundColor = [UIColor blackColor];
  22. [self.view addSubview:button1];
  23. // 注冊观察者
  24. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(returnAction:) name:@"chuanzhi" object:nil];
  25. // Do any additional setup after loading the view.
  26. }
  27. - (void)returnAction:(NSNotification *)text {
  28. text1.text = text.userInfo[@"returnInfo"];
  29. }
  30. - (void)pushButtonAction:(UIButton *)btn {
  31. ViewController2 *vc2 = [[ViewController2 alloc] init];
  32. vc2.modalPresentationStyle = UIModalPresentationFullScreen; //切换效果
  33. [self presentViewController:vc2 animated:YES completion:nil];
  34. }

ViewController2.m


  1. #import "ViewController2.h"
  2. @interface ViewController2 ()
  3. {
  4. UITextField *text2;
  5. }
  6. @end
  7. @implementation ViewController2
  8. - (void)viewDidLoad {
  9. [super viewDidLoad];
  10. self.view.backgroundColor = [UIColor blueColor];
  11. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 50)];
  12. label.text = @"界面二";
  13. [self.view addSubview:label];
  14. text2 = [[UITextField alloc] initWithFrame:CGRectMake(50, 150, 100, 50)];
  15. text2.backgroundColor = [UIColor whiteColor];
  16. [self.view addSubview:text2];
  17. UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(50, 250, 100, 50)];
  18. [button2 addTarget:self action:@selector(returnButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  19. [button2 setTitle:@"返回" forState:UIControlStateNormal];
  20. button2.backgroundColor = [UIColor blackColor];
  21. [self.view addSubview:button2];
  22. // Do any additional setup after loading the view.
  23. }
  24. - (void)returnButtonAction:(UIButton *)btn {
  25. NSDictionary *dic = @{@"returnInfo" : self->text2.text};
  26. NSNotification *notification = [NSNotification notificationWithName:@"chuangzhi" object:nil userInfo:dic];
  27. [[NSNotificationCenter defaultCenter] postNotification:notification];
  28. [self dismissViewControllerAnimated:NO completion:nil];
  29. }

通知模式实现两个textField传值及模态视图——iOS开发的更多相关文章

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

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

  2. iOS开发- 界面传值(1)-通知模式(广播)

    之后的几篇博客, 记录下不同界面间传值的经常使用办法. 这篇文章记录广播的方式. iOS的设计模式中,通知模式也是当中重要的模式之中的一个,Notification直译为通知,事实上本人认为叫做广播模 ...

  3. Android消息通知(notification)和PendingIntent传值

    通知栏的自定义布局:转:http://blog.csdn.net/vipzjyno1/article/details/25248021 拓展 实现自定义的通知栏效果: 这里要用到RemoteViews ...

  4. 探究Repository模式的两种写法与疑惑

    现如今DDD越来越流行,园子里漫天都是介绍关于它的文章.说到DDD就不能不提Repository模式了,有的地方也叫它仓储模式. 很多时候我们对Repository都还停留在Copy然后使用的阶段, ...

  5. iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)

    iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)   使用Block的地方很多,其中传值只是其中的一小部分,下面介绍Block在两个界面之间的传值: 先说一下思想: ...

  6. 内核知识第12讲,SSDT表.以用户模式到系统模式的两种方式.

    内核知识第12讲,SSDT表.以用户模式到系统模式的两种方式. 一丶IDT解析. 我们知道.IDT表中存放着各种中断信息.比如当我们调用int 3的时候,则会调用IDT表中的第三项来进行调用. 而函数 ...

  7. UIButton的两种block传值方式

    UIButton的两种block传值方式 方式1 - 作为属性来传值 BlockView.h 与 BlockView.m // // BlockView.h // Block // // Create ...

  8. 单例Singleton模式的两种实现方法

    在设计模式中,有一种叫Singleton模式的,用它可以实现一次只运行一个实例.就是说在程序运行期间,某个类只能有一个实例在运行.这种模式用途比较广泛,会经常用到,下面是Singleton模式的两种实 ...

  9. iOS开发笔记7:Text、UI交互细节、两个动画效果等

    Text主要总结UILabel.UITextField.UITextView.UIMenuController以及UIWebView/WKWebView相关的一些问题. UI细节主要总结界面交互开发中 ...

随机推荐

  1. 移动端利用iscroll实现小图变大图

    大图界面,使用iscroll,定义如下: var myScroll; function loaded(){ myScroll = new iScroll('wrapper', { zoom:true, ...

  2. 【python之旅】python的基础二

    一.集合的操作 1.什么是集合?     集合是一个无序的,不重复的数据组合,它的主要作用如下: 去重:把一个列表变成集合,就自动去重 关系测试:测试两组数据之前的交集,差集,并集等关系   2.常用 ...

  3. python 小记 整数与小数id

    上图,id A =B id 1.0  c != d 以后少用 带小数后位的数字.调用内存地址不一样

  4. 【python】【转】Python生成随机数的方法

    如果你对在Python生成随机数与random模块中最常用的几个函数的关系与不懂之处,下面的文章就是对Python生成随机数与random模块中最常用的几个函数的关系,希望你会有所收获,以下就是这篇文 ...

  5. RegularExpressionValidator 常用

    RegularExpressionValidator 控件用于验证输入值是否匹配正则表达式指定的模式 属性: ControlToValidate="要验证的控件名称" Valida ...

  6. TaskMgr C#技术拾遗

    1. DataGridView和ContextMenuStrip的绑定是发生在DataGridView的CellMouseClick事件,在事件中指定右键菜单弹出: 2. DataGridView的列 ...

  7. 2013Java最新面试题

    更新时间:2015-04-07         来源:网络         投诉删除 [看准网(Kanzhun.com)]Java面试题频道小编搜集的范文“2013Java最新面试题”,供大家阅读参考 ...

  8. TMS IntraWeb 5.4.1.1 for XE6 (适配Intraweb14.0.32)

    文件夹内含有我自己已经编译好的bpl,仅供大家学习使用,请支持正版!!导入ParaInstalarXE6.groupproj后,逐个编译安装即可. 链接:http://pan.baidu.com/s/ ...

  9. JSP环境配置

    为免以后忘记,记下了. Jdk在C盘,tomcat在D盘. 1.JAVA_HOME C:\Program Files\Java\jdk1.7.0_07 2.CATALINA_HOME D:\apach ...

  10. 纯CSS实现delay连续动画

    从前css3还没出来的时候,用jquery的delay方法可以串起一个一个独立的动画片段. 那么在不使用jquery的平台上,如何借助css3来完成一些列动作呢? 有高人做了一个动感十足的人物动画: ...