通知模式实现两个textField传值及模态视图——iOS开发
通知模式实现两个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开发的更多相关文章
- Objective-C(十九、通知-消息发送模式之中的一个)——iOS开发基础
结合之前的学习笔记以及參考<Objective-C编程全解(第三版)>,对Objective-C知识点进行梳理总结. 知识点一直在变.仅仅是作为參考.以苹果官方文档为准~ 十九.通知-消息 ...
- iOS开发- 界面传值(1)-通知模式(广播)
之后的几篇博客, 记录下不同界面间传值的经常使用办法. 这篇文章记录广播的方式. iOS的设计模式中,通知模式也是当中重要的模式之中的一个,Notification直译为通知,事实上本人认为叫做广播模 ...
- Android消息通知(notification)和PendingIntent传值
通知栏的自定义布局:转:http://blog.csdn.net/vipzjyno1/article/details/25248021 拓展 实现自定义的通知栏效果: 这里要用到RemoteViews ...
- 探究Repository模式的两种写法与疑惑
现如今DDD越来越流行,园子里漫天都是介绍关于它的文章.说到DDD就不能不提Repository模式了,有的地方也叫它仓储模式. 很多时候我们对Repository都还停留在Copy然后使用的阶段, ...
- iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)
iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值) 使用Block的地方很多,其中传值只是其中的一小部分,下面介绍Block在两个界面之间的传值: 先说一下思想: ...
- 内核知识第12讲,SSDT表.以用户模式到系统模式的两种方式.
内核知识第12讲,SSDT表.以用户模式到系统模式的两种方式. 一丶IDT解析. 我们知道.IDT表中存放着各种中断信息.比如当我们调用int 3的时候,则会调用IDT表中的第三项来进行调用. 而函数 ...
- UIButton的两种block传值方式
UIButton的两种block传值方式 方式1 - 作为属性来传值 BlockView.h 与 BlockView.m // // BlockView.h // Block // // Create ...
- 单例Singleton模式的两种实现方法
在设计模式中,有一种叫Singleton模式的,用它可以实现一次只运行一个实例.就是说在程序运行期间,某个类只能有一个实例在运行.这种模式用途比较广泛,会经常用到,下面是Singleton模式的两种实 ...
- iOS开发笔记7:Text、UI交互细节、两个动画效果等
Text主要总结UILabel.UITextField.UITextView.UIMenuController以及UIWebView/WKWebView相关的一些问题. UI细节主要总结界面交互开发中 ...
随机推荐
- C#快递单号查询源码
源码本人测试过,没有啥问题,能查询快递单号,支持的快递还挺多,圆通快递.申通快递.韵达快递的都支持单号查询的,程序是通过向爱快递(www.aikuaidi.cn)接口传输参数来查询快递单号,我直接把代 ...
- SpringMVC源码阅读(三)
先理一下Bean的初始化路线 org.springframework.beans.factory.support.AbstractBeanDefinitionReader public int loa ...
- 网页错误404 or 500
HTTP 错误 400 400 请求出错 由于语法格式有误,服务器无法理解此请求.不作修改,客户程序就无法重复此请求. HTTP 错误 401 401.1 未授权:登录失败 此错误表明传输给服务器的证 ...
- Epoll在LT和ET模式下的读写方式
在一个非阻塞的socket上调用read/write函数, 返回EAGAIN或者EWOULDBLOCK(注: EAGAIN就是EWOULDBLOCK) 从字面上看, 意思是:EAGAIN: 再试一次, ...
- QT事件过滤器(QT事件处理的5个层次:自己覆盖或过滤,父窗口过滤,Application过滤与通知)
Qt事件模型一个真正强大的特色是一个QObject 的实例能够管理另一个QObject 实例的事件. 让我们试着设想已经有了一个CustomerInfoDialog的小部件.CustomerInfoD ...
- nbtstat Linux版源码, 通过IP获取主机名
nbtstat Linux版, 通过IP获取主机名/* NETBIOS name lookup tool - by eSDee of Netric (www.netric.org) * yeh.. i ...
- android调用百度地图API
http://blog.csdn.net/lyq8479/article/details/6384428
- bzoj2165
类似于bzoj1706,考虑到楼层是等价的我们令f[p,i,j]为用了2^p次电梯,从房间i到j最多上升了多少层然后从2^p很容易推到2^(p+1),类似于floyd的转移即可下面我们要用最小的电梯次 ...
- Apache Hadoop RPC Authentication 安全绕过漏洞
漏洞名称: Apache Hadoop RPC Authentication 安全绕过漏洞 CNNVD编号: CNNVD-201308-425 发布时间: 2013-08-28 更新时间: 2013- ...
- Spark安装和配置
hadoop2的安装教程 Spark可以直接安装在hadoop2上面,主要是安装在hadoop2的yarn框架上面 安装Spark之前需要在每台机器上安装Scala,根据你下载的Spark版本,选择对 ...