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

posted on 2013-08-24 14:02  大米稀饭  阅读(988)  评论(0编辑  收藏  举报