AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m
#import "AppDelegate.h"
@interface AppDelegate ()
@property(nonatomic, retain)UITextField *secondText;
@property(nonatomic, retain)UITextField *textField;
@end
@implementation AppDelegate
- (void)dealloc {
[_window release];
[_secondText release];
[_textField release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[_window release];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 50)];
self.textField.backgroundColor = [UIColor yellowColor];
[self.window addSubview:self.textField];
[self.textField release];
// 边框
self.textField.layer.borderWidth = 1;
// 弧度
self.textField.layer.cornerRadius = 10;
// textField.text = @"hehehe和";
self.textField.textColor = [UIColor redColor];
self.textField.textAlignment = NSTextAlignmentCenter;
self.textField.font = [UIFont systemFontOfSize:21];
// [textField sizeToFit];
// textField.center = CGPointMake(100, 100);
self.textField.placeholder = @"请输入内容";
// 控制能否使用输入框
self.textField.enabled = YES;
// 密码效果
self.textField.secureTextEntry = NO;
// 键盘类型
// textField.keyboardType = UIKeyboardTypeNumberPad;
// 改变return按钮的样式
self.textField.returnKeyType = UIReturnKeyYahoo;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 100)];
view.backgroundColor = [UIColor yellowColor];
// 可以通过自定义的视图, 取代键盘
// textField.inputView = view;
// textField.inputAccessoryView = view;
// 清除按钮
self.textField.clearButtonMode = UITextFieldViewModeAlways;
// 给textFlield添加一个事件
[self.textField addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventEditingChanged];
[_textField release];
NSLog(@"%p", self.textField);
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 150, 50)];
label.layer.borderWidth = 1;
label.layer.cornerRadius = 10;
[self.window addSubview:label];
[label release];
label.tag = 1000;
// 控件写成属性, 一定要使用
self.secondText = [[UITextField alloc] initWithFrame:CGRectMake(100, 300, 150, 50)];
self.secondText.backgroundColor = [UIColor cyanColor];
[self.window addSubview:self.secondText];
[self.secondText release];
[self.secondText addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventEditingChanged];
NSLog(@"%p", self.secondText);
[_secondText release];
#warning 在给某些控件绑定方法的时候, 一般会在写方法的时候给同一个类型的参数, 哪个控件去执行方法, 对应的参数就是哪个对象, 省去寻找触发事件的对象的麻烦
return YES;
}
- (void)valueChange:(UITextField *)textfield {
// NSLog(@"%@", textfield);
// 先通过tag值找到指定的label
UILabel *label = (UILabel *)[self.window viewWithTag:1000];
// label.text = textfield.text;
if ([self.secondText.text isEqualToString:self.textField.text]) {
label.text = @"相同";
} else {
label.text = @"不同";
}
}