zoukankan      html  css  js  c++  java
  • 通知中心

    //
    //  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
    

    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    java url 获取文件_[转]从URL获取文件保存到本地的JAVA代码,url 请求设置http请求头
    解决Mybatis中出现的Invalid bound statement (not found)问题
    使用多线程往LIST添加数据 线程安全list
    spring BeanUtils 工具实现对象之间的copy 属性复制,属性拷贝
    idea 打开Service窗口一个管理所有服务的地方
    java实现大文件下载(http方式)
    Java @Override注解写与不写的区别
    Linux下C++共享内存
    Linux配置开机自启
    Linux中zsh出现 zsh: corrupt history file /XXX/.zsh_history解决办法
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4829643.html
Copyright © 2011-2022 走看看