UI设计编程:基础视图、程序启动流程
2015-09-09 22:02
495 查看
实际App中的登录界⾯面并⾮非是由一个一个⾊色块组成,⽽而是由标签(UILabel)、输⼊入框(UITextField)和按钮(UIButton)组成 UITextField是什么: UITextField(输入框):是控制文本输入和显示的控件。在App中UITextField出现频率也比较高。 iOS系统借助虚拟键盘实现输入,当点击输入框,系统会自动调出键盘,⽅便 你进一步操作。在你不需要输入的时候,可以使用收回键盘的方法,收回弹出的键盘。 UITextField和UILabel相比,UILabel主要用于文字显示,不能编辑,UITextField允许用户编辑文字(输入)。
如何使用UITextField
创建UITextField与创建UILabel的步骤很相似。(Label的创建步骤在上一篇中有提到)
1、开辟空间并初始化(如果本类有初始化⽅方法,使⽤用⾃自⼰己的;否则 使⽤用⽗父类的)。
2、设置⽂文本显⽰示、输⼊入相关的属性
3、添加到⽗父视图上,⽤用以显⽰示
4、释放
UITextField:代码如下:
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(100,100,200,30)]; textField.backgroundColor = [UIColor whiteColor]; //边框样式 //textField.borderStyle = UITextBorderStyleRoundedRect;//这个属性会跟UIImage起冲突 textField.borderStyle = UITextBorderStyleNone; //占位符(在文本框中显示内容,起到一个占位的作用) textField.placeholder = @"";//比如要输入密码,那这儿就写:“请输入密码” textField.tag = 100; //二次输入,清空上一次的文本内容 textField.clearsOnBeginEditing = YES; //设置背景图片,需要设置为UITextBorderStyleNone textField.background = [UIImage imageName:@""];//输入你要插入的图片的名称 //设置是否可以编辑 textField.enabled = YES; //设置密码(是否为圆点或者星号) textField.secureTextEntry = YES; //修改键盘样式 textField.keyboardType = UIKeyboardTypeEmailAddress; //设置键盘颜色 textField.keyboardAppearance = UIKeyboardAppearanceDark; //修改return键文字 textField.returnKeyType = UIReturnKeyJoin; //称为第一响应者,刚一进来,键盘就会弹出来 [textField becomeFirstResponder]; //下面这段代码是将键盘用一段红色的区域遮盖掉(可以在这片区域上自定义键盘样式) UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.window.frame.size.width,200)]; inputView.backgroundColor = [UIColor redColor]; textField.inputView = inputView; //在键盘的上面部分出现一部分蓝色区域 UIView *inputAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.window.frame.size.width,100)]; inputAccessoryView.backgroundColor = [UIColor blueColor]; textField.inputAccessoryView = inputAccessoryView; //清除按钮显示模式(当在编译的时候就会出现一个小X号) textField.clearButtonMode = UITextFieldViewModeWhileEditing; //左视图 UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0,0,30,30)]; leftView.backgroundColor = [UIColor redColor]; textField.leftView = leftView; textField.leftViewMode = UITextFieldViewModeAlways;
创建一个UIButton:代码如下
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(100,100,200,30); //设置按钮文字(有好多样式,你可以试试) [button setTitle:@"登录" forState:UIControlStateNormal]; //一直点着按钮的时候就会变成下面的title [butto setTitle:@"loading" forState:UIControlStateHighlighted]; //设置点击的时候是否有光照 button.showsTouchWhenHighlighted = YES; //给button(添加)绑定事件 [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; //添加了一个方法 -(void) buttonAction:(UIButton *)sender { /摁了button键后,文本框的内容就会显示在上一篇提到的label上 UITextField *textField = (UITextField *)[self.window viewWithTag:100]; UILabel *label = (UILabel *)[self.window viewWithTag:101]; label.text = textFied.text; }
delegate协议(点return回收键盘)
步骤:
1.将AppDelete作为UITextField的delegate
2.AppDelete.h文件接受UITextFieldDelegate协议
3.AppDelete.m文件实现textFieldShouldReturn:方法
#import "AppDelegate.h" //第二步:遵守协议 @interface AppDelegate ()<UITextFieldDelegate> @end @implementation AppDelegate -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //写一个UITextField //第一步:将当前类对象设置为输入框代理 textField.delegate = self; } //第三步:实现协议方法 -(BOOL)textFieldShouldReturn:(UITextField *)textField { //释放第一响应者(可以让键盘自动收放) [textField resignFirstResponder]; return YES; }
应用程序代理(UIApplicationDelegate)
!应用程序代理
*
总结
*UILabel、UITextField、UIButton都是特别常⽤用的控件。
他们都是直接或者间接继承于UIView,只是外观和功能不同。 熟悉各种控件的特点和⽅方法是我们后期做项⺫⽬目的保障。 delegate是iOS重要的设计模式,理解它的原理,有助于写出优质代码
main函数是程序的⼊入⼝口。UIApplicationMain函数创建了应⽤用程序实例,应 ⽤用程序代理实例,建⽴立事件循环。
相关文章推荐
- EFI,UEFI和操作系统
- AdaultBird--我的黑马程序员之路!Chapter7---File类,IO流里的其他工具类,GUI
- COCI2014silueta
- java parseInt和valueof的区别
- hdu 5014__Number Sequence
- ACE_Message_Queue
- UIPageControl
- UILabel
- UVA 11324 The Largest Clique (拆点+KM)
- iOS—实现UI imageview的底层
- Expression Tree Build
- 09-09 Service(续)、UI主线程、AsyncTask(UI主线程)
- Android Service——在子线程中更新UI
- iOS小结(五) 结合 Instrument 分析并解决memory issues
- interview questions
- ios-UIKit 练习
- UINavigationController和UIBarButtonItem
- UIView
- Qt5 中使用了QQuickWidget,程序退出时容易崩溃的解决办法。
- button初始化类目