UITextField(输入框):是控制文本输入和显示的控件.
UITextField和UILabel相比,UILabel主要用于文字显示,不能编辑, UITextField允许用户编辑文字(输入).
UITextField 的创建
创建UITextField与创建UILabel的步骤很相似。
1、开辟空间并初始化(如果本类有初始化⽅方法,使⽤用⾃自⼰己的;否则使⽤用⽗父类的)。
2、设置⽂文本显⽰示、输⼊入相关的属性
3、添加到⽗父视图上,⽤用以显⽰示
4、释放
UITextField *userNameTextField = [[UITextField
alloc] initWithFrame:CGRectMake(100, 100, 190, 30)];
userNameTextField.borderStyle =
UITextBorderStyleRoundedRect;
userNameTextField.placeholder=@"⼿手机号/邮箱";[containerViewaddSubview:userNameTextField];[userNameTextFieldrelease];
UITextField功能:1,文本显示,2,输入空中,3,外观配置
文本显示:
属性名:text 要显示的文本内容 eg:textField.text=@"你好";
属性名:textColor 文本内容的颜色 eg:textField.textColor =[UIColor redColor];
属性名:textAlignment 文本对齐方式 eg:textField.textAlignment =NSTextAlignmentLeft;
属性名:font 文本字体 eg:textField.font = [UIFont fontWithName@"Helvetica-Bold" size:20];//黑体加粗,20号字.
属性ming:placeholder 占位字符串(没有任何输入的时候,给出的提示字符串) eg:textField.placeholder = @"请输入用户名";
输入控制:
enabled 是否允许输入 textField.enabled=NO;//不允许输入,不弹出键盘 textField.enabled = YES;//默认是YES.允许输入.
clearsOnBeginEditing 是否开始输⼊入的时候清空输⼊入框内容 textField.clearsOnBeginEditing = YES;//清空 textField.clearsOnBeginEditing = NO;//不清空
secureTextEntry 是否⽂文字以圆点格式显⽰示 textField.secureTextEntry = YES;//密码式
textField.secureTextEntry = NO;//普通模式
创建UIButton与创建UILabel、UITextField、UIView的步骤很相似。
1、创建button对象(如果本类有初始化⽅方法,使⽤用⾃自⼰己的;否则使⽤用⽗父类的)。
2、设置按钮显⽰示相关的属性
3、为按钮添加点击事件
4、添加按钮到⽗父视图上,⽤用以显⽰示
5、按钮⽆无需释放(因为使⽤用的是类⽅方法创建的button)
UIButton *loginButton = [UIButton
buttonWithType:UIButtonTypeSystem];
loginButton.frame = CGRectMake(30, 200, 60, 30);
[loginButton setTitle:@"登录"forState:UIControlStateNormal];
[loginButton addTarget:self action:@selector(login:)
forControlEvents:UIControlEventTouchUpInside];
[containerView addSubview:loginButton];
[loginButton removeTarget:selfaction:@selector(login:)forControlEvents:UIControlEventTouchUpInside];
[loginButton setImage:[UIImage
forState:UIControlStateNormal];
imageNamed:@“login.png”]
NSString *normalTitle = [loginButtonforState:UIControlStateNormal];
imageForState 获取指定状态下的前景图片
UIImage *normalImage = [loginButton[loginButton setTitleColor:[UIColor
imageForState:UIControlStateNormal];
setBackgroundimage:forState: 设置指定状态下的背景图片
[loginButton setBackgroundImage:UIColor *normalTitleColor = [loginButton
[UIImage imageNamed:@“login2.png”]titleColorForState:UIControlStateNormal];
forState:UIControlStateNormal];
backgroundimageForState: 获取指定状态下的背景图片
UIImage *normalBackgroundImage =redColor] forState:UIControlStateNormal];
[loginButton
UIColor *normalTitleShadowColor =
backgroundImageForState:UIControlStateN[loginButton
ormal];
UIView 是所有可视化控件的基类
UILabel UITextField UIButton 是具有特定外观特定功能的视图
UILabel 侧重于文本的呈现
UITextField 侧重于输入
UIButton 侧重于点击事件处理
delegate
点return 回收键盘
步骤:
1.将AppDelete作为UITextField的delegate
2.AppDelete.h文件接受UITextFieldDelegate协议
3.AppDelete.m文件实现textFieldShouldReturn: 方法
键盘回收代码
AppDelegate.m
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
ios程序启动流程
任何⼀一个程序,⽆无论是基于Mac OS还是iOS,程序都是从main.m⽂文
件的main函数开始执⾏行的。
int main(int argc, char * argv[]) {
@autoreleasepool { return UIApplicationMain(argc, argv, nil,
NSStringFromClass([AppDelegate class])); }
}
执⾏行UIApplicationMain函数时做了跳转,转到了AppDelete中
UIApplicationDelegate:
application:didFinishLaunchingWithOptions: 告诉delegate程序启动即将完成,程序准备要运行,(delegate实现这个⽅方法时,要创建window对象,将程序内容通过window呈现给⽤用户。)
| |
applicationDidBecomeActive: |
告诉delegate应⽤用程序已经进⼊入活跃状态(重新执⾏行被暂停的任务) |
applicationWillResignActive: |
告诉delegate应⽤用程序即将进⼊入⾮非活跃状态(暂停游戏、停⽌止timer等) |
applicationDidEnterBackground: |
告诉delegate应⽤用程序已经进⼊入到了后台(存储⽤用户数据、释放⼀一些共享资源、停⽌止timer等) |
applicationWillEnterForeground: |
告诉delegate应⽤用程序即将进⼊入前台(取消所有进⼊入后台时暂停的任务) |
UIWindow :
UIWindow作为应用程序的窗口,在应用程序启动的时候就要加载,各种UI界面都是协助window来呈现的.
UIWindow在创建的时候,应该和屏幕一样大.通过[UIScreen mainScreen].bounds 可以获得屏幕的大小.
[self.window makeKeyAndVisible];// 让window变得可视,即显示window.
UILabel、UITextField、UIButton都是特别常⽤用的控件。
他们都是直接或者间接继承于UIView,只是外观和功能不同。熟悉各种控件的特点和⽅方法是我们后期做项⺫⽬目的保障。delegate是iOS重要的设计模式,理解它的原理,有助于写出优质代码
main函数是程序的⼊入⼝口。UIApplicationMain函数创建了应⽤用程序实例,应⽤用程序代理实例,建⽴立事件循环。