//
// 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 {
// 用户名
_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 = @[@"登录名:", @"密码:", @"请输入用户名", @"请输入密码", @"登录", @"取消"];
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
原文地址:http://blog.csdn.net/u010606986/article/details/44457757