AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m
#import "AppDelegate.h"
@interface AppDelegate ()<UITextFieldDelegate>
@property(nonatomic, assign)BOOL isSelected;
@property(nonatomic, retain)UITextField *textField;
@end
@implementation AppDelegate
- (void)dealloc {
[_window 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];
// 创建一个Button
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 100, 150, 50);
button.backgroundColor = [UIColor yellowColor];
[self.window addSubview:button];
// Button用便利构造器创建, 不需要释放
// 添加边框, 添加弧度
button.layer.borderWidth = 1;
button.layer.cornerRadius = 10;
// 添加一个标题
[button setTitle:@"确认" forState:UIControlStateNormal];
// 修改字体大小
button.titleLabel.font = [UIFont systemFontOfSize:25];
// 修改颜色
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// 绑定一个点击方法
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
// 创建一个textField
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 150, 50)];
self.textField.layer.borderWidth = 1;
self.textField.layer.cornerRadius = 10;
[self.window addSubview:self.textField];
[_textField release];
self.textField.secureTextEntry = YES;
// 设置代理人
self.textField.delegate = self;
return YES;
}
// 点击return, 回收键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(@"1111111");
// 失去第一响应者
[textField resignFirstResponder];
return YES;
}
- (void)click:(UIButton *)button {
if ([button.currentTitle isEqualToString:@"确认"]) {
NSLog(@"%@", self.textField.text);
[button setTitle:@"取消" forState:UIControlStateNormal];
} else {
[button setTitle:@"确认" forState:UIControlStateNormal];
}
self.textField.secureTextEntry = !self.textField.secureTextEntry;
}