#import "AppDelegate.h"
@interface AppDelegate () <UITextFieldDelegate>
@property (nonatomic,assign) BOOL flag;
// 将控件视图写成属性的好处就是在这个类内部可以随时获取对象取值赋值
@property (nonatomic,retain) UITextField *textField;
@end
@implementation AppDelegate
- (void)dealloc
{
[_textField release];
[_window 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(20, 20, 335, 40)];
self.textField.backgroundColor = [UIColor yellowColor];
self.textField.tag = 10000;
// 设置textField的代理人
_textField.delegate = self;
[self.window addSubview:self.textField];
[_textField release];
self.textField.placeholder = @"请输入用户名";
self.textField.borderStyle = UITextBorderStyleRoundedRect;
self.textField.clearButtonMode = UITextFieldViewModeAlways;
// textField.secureTextEntry = YES;
self.textField.keyboardType = UIKeyboardTypeURL;
// UIButton
//使用button的遍历构造器的方法进行创建, 便利构造器,不用释放内存
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
// title
button.backgroundColor = [UIColor yellowColor];
//位置
button.frame = CGRectMake(20, 120, 335, 40);
[self.window addSubview:button];
//给button添加标题
[button setTitle:@"title" forState:UIControlStateNormal];
//
button.showsTouchWhenHighlighted = YES;
// 给button指定一个功能(方法)
// 参数1: 点击按钮之后,让什么对象调用方法
// 参数2: 让参数1的对象调用的方法
// 参数3: 按钮的事件(点击,拖动....)
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//给button设置图片 //图片名为"zz.png"
[button setBackgroundImage:[UIImage imageNamed:@"zz.png"] forState:UIControlStateNormal];
//setBackgroundImage方法会把图片铺满整个button
//隐藏多余的部分
button.layer.masksToBounds = YES;
UIButton *switchButton = [UIButton buttonWithType:UIButtonTypeCustom];
switchButton.frame = CGRectMake(20, 300, 50, 50);
//添加图片
//如果要使用setImage,button类型必须是Custom
[switchButton setImage:[UIImage imageNamed:@"zz.png"] forState:UIControlStateNormal];
[self.window addSubview:switchButton];
[switchButton addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];
return YES;
}
- (void)changeImage:(UIButton *)btn
{
}
// 实现textField的协议方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// 当用户点击textField的时候,要不要开始编辑状态
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
// 是否允许结束编辑状态
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
textField.text = @"不要乱写啊啊啊啊啊";
NSLog(@"已经开始编辑");
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
return YES;
}
// 按钮点击方法的写法格式
// - (void)方法名:(UIButton *)btn
- (void)buttonAction:(UIButton *)btn
{
NSLog(@"点击了");
//1.
// if (self.window.backgroundColor == [UIColor whiteColor]) {
// self.window.backgroundColor = [UIColor blackColor];
// }else{
//
// self.window.backgroundColor = [UIColor whiteColor];
// }
// 2.用属性
// if (self.flag == NO) {
// self.window.backgroundColor = [UIColor blackColor];
// }else{
// self.window.backgroundColor = [UIColor whiteColor];
// }
// self.flag = !self.flag;
// 父类的指针可以指向子类的对象,反过来不可以
//用标记
// UITextField *field = (UITextField *)[self.window viewWithTag:10000];
// if ([field.text isEqualToString:@"red"]) {
// self.window.backgroundColor = [UIColor redColor];
// }
//用属性
if ([_textField.text isEqualToString:@"red"]) {
_textField.text = @"blue";
self.window.backgroundColor = [UIColor blueColor];
}
// 让textField键盘回收
[_textField resignFirstResponder];
}