#import "AppDelegate.h"
@interface AppDelegate ()
@property (nonatomic, assign)BOOL flag;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@"开始启动,并加载完毕");
NSLog(@"%s", __func__);
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];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 80, 150, 50)];
textField.text = @"Bob";
textField.placeholder = @"请输入姓名";
textField.borderStyle = UITextBorderStyleRoundedRect;
// 系统的协议使用 2. 指定代理人
textField.delegate = self;
// 设置textField的标签,方便父视图查找
textField.tag = 1000;
textField.clearButtonMode = UITextFieldViewModeAlways;
textField.clearsOnBeginEditing = YES;
textField.backgroundColor = [UIColor grayColor];
[self.window addSubview:textField];
[textField release];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(20, 150, 80, 80);
button.backgroundColor = [UIColor brownColor];
[button setTitle:@"点击" forState:UIControlStateNormal];
button.showsTouchWhenHighlighted = YES;
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:button];
return YES;
}
// textField的协议方法实现
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// 当textField被点击的时候,是否开始编辑状态
return YES;
}// return NO to disallow editing
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"已经开始编辑");
}// became first responder
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSLog(@"%@", string);
if ([string isEqualToString:@"A"]) {
return YES;
}
return NO;
}// return NO to not change text
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
// 当点击键盘的return键的时候,会调用这个方法
[textField resignFirstResponder];
return YES;
}
// 按钮点击方法
- (void)buttonClicked:(UIButton *)button
{
NSLog(@"点击");
// 通过父视图和tag值搜索某一个子视图
UITextField *textF = (UITextField *)[self.window viewWithTag:1000];
// 让textField结束编辑状态,回收键盘
[textF resignFirstResponder];
if ([textF.text isEqualToString:@"AAA"]) {
[button setTitle:textF.text forState:UIControlStateNormal];
}
}
- (void)dealloc
{
[_window release];
[super dealloc];
}