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