通知模式实现两个textField传值及模态视图——iOS开发
来源:互联网 发布:新东方留学怎么样知乎 编辑:程序博客网 时间:2024/06/05 23:42
通知模式实现两个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];}
0 0
- 通知模式实现两个textField传值及模态视图——iOS开发
- iOS开发——视图及视图控制器
- iOS开发- 界面传值(1)-通知模式(广播)
- 【iOS开发】观察者模式-通知的使用及深入理解
- iOS开发-模式视图
- iOS雨笙 NSNotification通知模式的原理及实现
- ios开发-通知传值
- iOS开发 通知传值
- 【iOS开发】UILocalNotification 本地通知的实现 —— 伊禾媛
- IOS两个视图之间的传值
- iOS开发——真·高度自适应的TextField
- iOS开发中通知(Notification)快速入门及推送通知实现教程
- iOS项目开发实战——实现视图切换动画
- iOS--界面传值 通知模式(广播)
- ios开发——日常之iOS通知传值的使用
- iOS开发 使用Block实现两个页面互相传值
- ios开发——NSNotificationCenter (通知中心)
- Objective-C(十九、通知-消息发送模式之一)——iOS开发基础
- 动画中的模块化设计
- 日经社説 20150712 いまこそ暴力団への資金断て
- POJ 3086 Triangular Sums(水~)
- 日经社説 20150712 中ロに問われる国際秩序づくりの自覚
- javascript设置input编辑框光标的位置
- 通知模式实现两个textField传值及模态视图——iOS开发
- 字符串的应用
- 社説 20150712 岩手中2生自殺 なぜSOSは届かなかったか
- 项目管理工具Maven
- 改变Ubuntu下作为桌面,下载存放位置等的默认文件夹
- IOS - 0 -今天开始学IOS开发
- 社説 20150712 核燃料サイクル 政府の責任で軌道に乗せよ
- 常见js效果——在表格中收起展开
- LeetCode #9 Palindrome Number