IOS控件:计算文字长度(UITextField,UILabel对象 和 IBAction)

#import <UIKit/UIKit.h>

// UIViewController类为程序提供了基本的视图管理模块
@interface NavControllerViewController : UIViewController {
    // 此处建立标签显示文字;把所需显示的文字标签告诉Interface Builder
    IBOutlet UILabel *label;
    // 文本框;把所需显示的文字标签告诉Interface Builder
    IBOutlet UITextField *textField;
}
// @property 声明程序的属性
@property(nonatomic, retain) UITextField *textField;
@property(nonatomic, retain) UILabel *label;
// 操作效应的方法,单击按钮把文本框输入的文字长度值显示在标签控件上
-(IBAction)click;

@end

 

#import "NavControllerViewController.h"

@interface NavControllerViewController ()

@end

@implementation NavControllerViewController

// 本指令告诉编译器去合成方法所需要的“文本框”和“标签”控件存储方法
@synthesize textField, label;

-(IBAction)click {
    int textCount = textField.text.length;
    if (textCount > 30) {
        label.text = @"您输入的信息,长度不能超过30个字母。";
        textField.text = NULL;
    }else{
        NSString *result = [NSString stringWithFormat:@"您输入的文字长度为:%d", textCount];
        label.text = result;
        textField.text = NULL;
    }
}

// 释放方法,执行内存清理工作
- (void)dealloc{
    [label release];
    [textField release];
    [super dealloc];
}

- (void)viewDidLoad {
    label.text = @"请输入文字";
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

// 释放可用内存供给给应用程序,并提早发出警告提示
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

转载于:https://www.cnblogs.com/androidsj/p/3279275.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值