通知中心
2015-09-22 17:37
211 查看
// // KMainViewController.m // NotificationCenter // // Created by xiaoyao on 15/3/18. // Copyright (c) 2015年 lije. All rights reserved. // #import "KMainViewController.h" #import "LoginViewController.h" #define UPDATE_LOGIN_NOTIFICATION @"updateLoginInfo" @interface KMainViewController () { UILabel *_infoLabel; UIButton *_loginOutButton; } @end @implementation KMainViewController - (void)viewDidLoad { [super viewDidLoad]; [self setupUI]; } - (void)setupUI { // username _infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 100, 50)]; _infoLabel.textColor = [UIColor blueColor]; _infoLabel.textAlignment = NSTextAlignmentLeft; [self.view addSubview:_infoLabel]; _loginOutButton = [UIButton buttonWithType:UIButtonTypeCustom]; _loginOutButton.frame = CGRectMake(100, 300, 100, 50); [_loginOutButton setTitle:@"登录"forState:UIControlStateNormal]; [_loginOutButton addTarget:self action:@selector(logOut:) forControlEvents:UIControlEventTouchUpInside]; [_loginOutButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [self.view addSubview:_loginOutButton]; } - (void)logOut:(UIButton *)buttton { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLoginInfo:) name:UPDATE_LOGIN_NOTIFICATION object:nil]; LoginViewController *login = [[LoginViewController alloc] init]; [self presentViewController:login animated:YES completion:nil]; } - (void)updateLoginInfo:(NSNotification *)notifidcation { if (notifidcation) { _infoLabel.text = notifidcation.userInfo[@"loginInfo"]; [_loginOutButton setTitle:@"注销" forState:UIControlStateNormal]; } } // 移除监听 - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } @end // // LoginControllerController.m // NotificationCenter // // Created by xiaoyao on 15/3/18. // Copyright (c) 2015年 lije. All rights reserved. // #import "LoginViewController.h" #define UPDATE_LOGIN_NOTIFICATION @"updateLoginInfo" @interface LoginViewController () { UITextField *_userNameTextField; UITextField *_passwordTexttField; } @end @implementation LoginViewController - (void)viewDidLoad { [super viewDidLoad]; [self configureLayoutqLoginUI]; } - (void)configureLayoutqLoginUI { NSArray *titlrArray = @[@"登录名:", @"密码:", @"请输入username", @"请输入密码", @"登录", @"取消"]; CGFloat x = 20; CGFloat y = 200; CGFloat width = 80; CGFloat height = 50; for (int i = 0; i < titlrArray.count / 3; i++) { // 登录和密码 UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)]; titleLabel.text = [titlrArray objectAtIndex:i]; titleLabel.textColor = [UIColor blueColor]; titleLabel.textAlignment = NSTextAlignmentCenter; [self.view addSubview:titleLabel]; UITextField *infoTextField = [[UITextField alloc] initWithFrame:CGRectMake(x + width + 10, y, width + 100, height)]; infoTextField.textAlignment = NSTextAlignmentLeft; infoTextField.placeholder = [titlrArray objectAtIndex:i + 2]; infoTextField.clearButtonMode = UITextFieldViewModeWhileEditing; infoTextField.backgroundColor = [UIColor grayColor]; infoTextField.tag = 2000 + i; [self.view addSubview:infoTextField]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(x + width + 10 + (50 + 20) * i, 320, 60, 50); [button setTitle:[titlrArray objectAtIndex:i + 4] forState:UIControlStateNormal]; [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; button.tag = 1000 + i; [self.view addSubview:button]; y = titleLabel.frame.origin.y + titleLabel.frame.size.height + 10; } } - (void)buttonClicked:(UIButton *)button { _userNameTextField = (UITextField *)[self.view viewWithTag:2000]; _passwordTexttField = (UITextField *)[self.view viewWithTag:2001]; if(button.tag == 1000) { if ([_userNameTextField.text isEqualToString:@"lije"] && [_passwordTexttField.text isEqualToString:@"123"]) { [self postNotification]; } else { return; } } } - (void)postNotification { NSDictionary *infoDict = @{@"loginInfo" : [NSString stringWithFormat:@"Hello%@",_userNameTextField.text]}; [[NSNotificationCenter defaultCenter] postNotificationName:UPDATE_LOGIN_NOTIFICATION object:self userInfo:infoDict]; [self dismissViewControllerAnimated:YES completion:nil]; } @end
相关文章推荐
- Node.js中的DNS模块
- LAMP环境搭建
- 无锁编程(三) - 忙等待
- hdfs 上面block有异常处理流程
- leetcode Binary Tree Zigzag Level Order Traversal
- 无锁编程(三) - 忙等待
- BIEE Demo(RPD创建 + 分析 +仪表盘 )
- 实现页面侧滑的两种方式GestureDetector和ViewPager
- JS function 传递时间参数无法读取的问题
- HDU 5454 Excited Database 线段树的维护
- 让Hadoop跑在云端系列文章
- Cocos2d-x 3.2打包错误总结
- C#中循环获取枚举中的名称和值
- CLR 项目中 使用TimeSpan 计算时间差
- vs .sln与.suo文件
- 突发奇想:做一个D客户端(首先要深刻考察D才行啊)
- Rafy 框架-发布网页版用户手册
- 给body设置height:100%不起作用?
- mysql主主复制+keepalived实现高可用
- hdoj 1016 Prime Ring Problem【DFS】