zoukankan      html  css  js  c++  java
  • iOS-UIkit复习和代理的使用实现文本框限制输入字数控制

    //
    //  HMViewController.m
    //  01-UIKit复习
    //
    //  Created by apple on 14-8-18.
    //  Copyright (c) 2014年 itcast. All rights reserved.
    //
    
    #import "HMViewController.h"
    
    @interface HMViewController () <UITextFieldDelegate>
    
    @end
    
    @implementation HMViewController
    
    /**
     1> UIButton    -> UIControl -> UIView
     
     1.1 设置控件的状态
     
     启用、禁用
     @property(nonatomic,getter=isEnabled) BOOL enabled;
     选中、不选中
     @property(nonatomic,getter=isSelected) BOOL selected;
     高亮或者不高亮
     @property(nonatomic,getter=isHighlighted) BOOL highlighted;
     
     1.2 设置控件内容的布局
     垂直居中方向
     @property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment;
     水平居中方向
     @property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment;
     
     1.3 添加/删除监听方法
     - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
     - (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
    
     2> UILabel     -> UIView
     3> UIImageView -> UIView
     4> UITextField -> UIControl
     
     *** 代理设计模式,在OC中,使用最为广泛的一种设计模式
     
     1> 代理的用处是什么?
     *  监听那些不能通过addTarget监听的事件!
     *  主要用来负责在两个对象之间,发生某些事件时,来传递消息或者数据
     
     2> 代理的实现步骤
     (1)    成为(子)控件的代理,父亲(控制器)成为儿子(文本框)的代理
     (2)    遵守协议->利用智能提示,快速编写代码
     (3)    实现协议方法
     */
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
        btn.center = self.view.center;
        [self.view addSubview:btn];
        
        // 将监听方法,注册到"运行循环",当按钮被点击后,"运行循环"通知视图控制器执行@selector的方法
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        
    
    //    UITextField *textFiled = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, 280, 40)];
    }
    
    - (void)click:(UIButton *)btn
    {
        NSLog(@"%s", __func__);
        
        [btn removeTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    #pragma mark - 文本框代理方法
    /**
     成为代理之后要做的事情,以及如何工作
     
     1> 协议:预先定义的一些方法名,每个方法对应不同的事件,但是没有具体的实现
    
     */
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        NSLog(@"%@ %@", NSStringFromRange(range), string);
        
        // 限制输入的长度
        int loc = range.location;
        return (loc < 6);
        
    //    if (loc < 6) {
    //        return YES;
    //    } else {
    //        return NO;
    //    }
        
        // 如果返回NO,就不向文本框中添加字符
    //    return YES;
    }
    
    @end
  • 相关阅读:
    Springboot源码 bean的注册
    Vue源码之 watch
    Vue源码之 slot
    Vue computed 的嵌套
    Vue的子组件绑定的方法中传入自定义参数
    Vue源码之 Vue的生命周期
    Vue源码之-----computed
    Vue源码之----为什么Vue中Array的pop,push等方法可以reactive,而Array[0]='a'这样的方法不会reactive?
    ReSharper 8.1支持Visual Studio 2013的特色——超强滚动条
    Python开发环境Wing IDE使用教程:部分调试功能介绍
  • 原文地址:https://www.cnblogs.com/zhuyaguang/p/4701389.html
Copyright © 2011-2022 走看看