当前位置:编程学习 > wap >>

用Button连接TextField和Label

viewController.h文件:
 
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@interface ViewController : UIViewController
 
{
    //建立文本框
    IBOutlet UITextField *textField;
    //建立标签显示文字
    IBOutlet UILabel *label;    
}
@property(nonatomic, retain) UITextField *textField;
@property(nonatomic, retain) UILabel *label;
-(IBAction)Click:(id)sender;
@end
 
ViewController.m文件:
 
#import "ViewController.h"
@implementation ViewController
@synthesize textField, label;
 
- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    // 把已经读取的Label标签的文字替换成为本程序的显示内容
    label.text = @"请输入文字";    
}
 
-(IBAction)Click:(id)sender
{
    int textCount = textField.text.length;
    //当长度大于30
    if (textCount > 30) {
        //输出结果
        label.text = @"Invalid Inputs";
        //输入文字清空
        textField.text = NULL;
    }
    // 如果长度不大于30
    else {
        //输出结果
        NSString *result = [NSString stringWithFormat:@"输入长度为:%d", textCount];
        label.text = result;
        //清空文字
        textField.text = NULL;
    }
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}
 
-(void) didReceiveMemoryWarning 
{
    [super didReceiveMemoryWarning];
}
 
//释放程序使用过的标签
-(void) dealloc
{
    [label release];
    [textField release];
    //执行内存清理
    [super dealloc];
}
@end
补充:移动开发 , IOS ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,