您的位置:首页 > 产品设计 > UI/UE

UI第二天:第⼆讲:基础视图、程序启动流程

2015-11-11 21:47 357 查看
⼀、UITextField
UITextField(输⼊框):是控制⽂本输⼊和显⽰的控件。
在App中UITextField 出现频率也⽐较⾼。
iOS系统借助虚拟键盘实现输⼊,当点击输⼊框,系统会⾃动调出键盘,⽅便 你进⼀步操作。
在你不需要输⼊的时候,可以使⽤收回键盘的⽅法,收回弹出的 键盘。
UITextField和UILabel相⽐,UILabel主要⽤于⽂字显⽰,不能编辑, UITextField允许⽤户编辑⽂字(输⼊)。
创建UITextField与创建UILabel的步骤很相似。
1、开辟空间并初始化(如果本类有初始化⽅法,使⽤⾃⼰的;否则 使⽤⽗类的)。
2、设置⽂本显⽰、输⼊相关的属性
3、添加到⽗视图上,⽤以显⽰
4、释放
UITextField *userNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 190, 30)];

userNameTextField.borderStyle = UITextBorderStyleRoundedRect;
userNameTextField.placeholder = @"⼿机号/邮箱";
[containerView addSubview:userNameTextField];
[userNameTextField release];
UITextField核⼼功能主要包含3个⽅⾯:
⽂本显⽰
输⼊控制
外观配置
⽂本显⽰



输⼊控制



外观控制



//初始化

UITextField *text= [[UITextField
alloc]initWithFrame:CGRectMake(100,
100,
200,
50)];

//字体颜色

text.backgroundColor = [UIColor
clearColor];

text.textColor = [UIColor
tangerineColor];

// 对齐方式

text.textAlignment =
NSTextAlignmentLeft;

//字体大小

text.font = [UIFont
systemFontOfSize:20];

//占位字

text.placeholder =
@"请输入";

//边框格式

text.borderStyle =
UITextBorderStyleRoundedRect;

//清楚按钮
默认NO

text.clearButtonMode =
UITextFieldViewModeWhileEditing;

// 是否允许输入

text.enabled =
YES;

// 是否开始输入的时候清空输入框内容

text.clearsOnBeginEditing =
YES;

//密文输入

text.secureTextEntry =
YES;

// 弹出键盘类型

// text.keyboardType = UIKeyboardTypeNumberPad;

// return设置

text.returnKeyType =
UIReturnKeyNext;

// UIView *inview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 300)];

// inview.backgroundColor = [UIColor qianweise];

// //自定义键盘

// text.inputView = inview;

//

// UIView *inview1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];

// inview1.backgroundColor = [UIColor qianweise];

// //辅助视图

// text.inputAccessoryView = inview1;

// [inview1 release];

//输入左视图

// text.leftView = inview;

// text.leftViewMode = UITextFieldViewModeAlways;

// [inview release];

// 设置代理(方法在那里实现的
就把谁设置为代理)
text.delegate = self;
//
实现协议的方法

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

// 代理可以有很多个点那个那个就是textfield

textField.backgroundColor = [UIColor colorWithRed:arc4random()%255/256.0 green:arc4random()%255/256.0 blue:arc4random()%255/256.0 alpha:1];

// 键盘回收
取消第一响应者

[textField resignFirstResponder];

return YES;
}
⼆、UIButton
UIButton(按钮):是响应⽤户点击的控件。在App中UIButton是出 现频率很⾼的控件。
UIButton与UILabel、UITextField侧重点不同,侧重于处理点按。当 然UIButton类也提供了⼀些⽅法控制按钮外观。
创建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]
UIButton添加事件



外观控制





UIView是所有可视化控件的基类。
UILabel、UITextField、UIButton是具有特定外观特定功能的视图。
UILabel侧重于⽂本的呈现。
UITextField侧重于输⼊。
UIButton侧重于点击事件处理。
UIButton *b = [UIButton
buttonWithType:UIButtonTypeCustom];

b.frame =
CGRectMake(100,
100,
100,
100);

b.backgroundColor = [UIColor
clearColor];

//设置标题

[b setTitle:@"普通"
forState:(UIControlStateNormal)];

[b setTitle:@"高亮"
forState:(UIControlStateHighlighted)];

[b setTitle:@"选中"
forState:(UIControlStateSelected)];

[b setTitleColor:[UIColor
redColor]
forState:UIControlStateNormal];

[b setTitleColor:[UIColor
blackColor]
forState:UIControlStateHighlighted];

[b setTitleColor:[UIColor
blueColor]
forState:UIControlStateSelected];

[b addTarget:self
action:@selector(button:)
forControlEvents:(UIControlEventTouchUpInside)];

// 如果不是png格式的图片加上后缀

[b setBackgroundImage:[UIImage
imageNamed:@"Normal.png"]
forState:UIControlStateNormal];

[b setBackgroundImage:[UIImage
imageNamed:@"Highlighted.png"]
forState:UIControlStateHighlighted];

[b setBackgroundImage:[UIImage
imageNamed:@"Selected.png"]
forState:UIControlStateSelected];
[self.window addSubview:b];
-(void)button:(UIButton *)button

{

button.selected = !button.selected;

// button.backgroundColor = [UIColor colorWithRed:arc4random()%255/256.0 green:arc4random()%255/256.0 blue:arc4random()%255/256.0 alpha:1];
}

三、delegate
点return回收键盘
点击键盘return按钮实现键盘回收的步骤:
1、将AppDelete作为UITextField的delegate
2、AppDelete.h⽂件接受UITextFieldDelegate协议
3、AppDelete.m⽂件实现textFieldShouldReturn:⽅法
AppDelegate.m
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{ [textField resignFirstResponder]; return YES; }
UITextField不应该在类内部(.m⽂件)实现textFieldShouldReturn:, 应为有时候,我们点return的时候,并不总是想回收键盘,例如:有 两个输⼊框,第⼀个输⼊框输⼊完成之后,⽤户点return按钮,将光 标移动到第⼆个输⼊框(即:第⼆个输⼊框称为第⼀响应者)

对于⼀个V来说,⾃⼰只负责触发事件,事件由外界实现,即 delegate。

四、程序启动流程
main函数是程序⼊⼝
任何⼀个程序,⽆论是基于Mac OS还是iOS,程序都是从main.m⽂ 件的main函数开始执⾏的。
int main(int argc, char * argv[])
{
@autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

}
} 执⾏UIApplicationMain函数时做了跳转,转到了AppDelete中
UIApplicationMain
UIApplicationMain在程序⼊⼝函数main函数中调⽤,主要实现了3个 功能:
创建应⽤程序(UIApplication)实例
创建应⽤程序代理实例
建⽴事件循环(runloop:死循环,不断检测程序运⾏状态,是否被触 摸、晃动等)
UIApplicationMain剖析
int UIApplicationMain ( int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName );

1、principalClassName:应⽤程序对象的类名(UIApplication或其⼦类)
2、delegateClassName:应⽤程序delegate的类名。(任何接受了 UIApplicationDelegate的类)
UIApplicationMain根据上述两个类名创建应⽤程序实例、应⽤程序代理实例。
然后建⽴事件循环(runloop),检测程序的各种事件(程序开始启动,接收到触 摸等等)
应⽤程序代理
应⽤程序代理,主要检测应⽤程序的状态并做出相应的处理。
应⽤程序的状态有很多,⽐如:程序启动、进⼊活跃状态、进到后 台、内存警告、收到远程消息等等
任何接受了UIApplicationDelegate协议的对象都可以成为应⽤程序 代理。
⼀旦应⽤程序的某种状态触发,就会执⾏相应的代理⽅法。
UIApplicationDelegate
UIApplicationDelegate是⼀个OC的协议。
⾥⾯声明了⼀堆⽅法,这些⽅ 法都与应⽤程序运⾏状态有关,它们由应⽤程序代理实现。UIApplication 对象负责调⽤。
UIApplicationDelegate



在AppDelete.m各个代理⽅法⾥打印log,查看各个代理⽅法的 执⾏顺序和执⾏顺序。
NSLog(@"%s %d",__FUNCTION__,__LINE__);
- (void)applicationWillResignActive:(UIApplication
*)application {

// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user
quits the application and it begins the transition to the background state.

// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

// _FUNCION_打印
调用了那个方法

// _LINE_ 打印
这个方法在多少行

NSLog(@"程序小退");

NSLog(@"%s %d",
__FUNCTION__,__LINE__);

}

- (void)applicationDidEnterBackground:(UIApplication
*)application {

// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated
later.

// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

NSLog(@"程序进入后台");

NSLog(@"%s %d",
__FUNCTION__,__LINE__);

}

- (void)applicationWillEnterForeground:(UIApplication
*)application {

// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

NSLog(@"程序进入前台");

NSLog(@"%s %d",
__FUNCTION__,__LINE__);

}

- (void)applicationDidBecomeActive:(UIApplication
*)application {

// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

NSLog(@"程序激活");

NSLog(@"%s %d",
__FUNCTION__,__LINE__);

}

- (void)applicationWillTerminate:(UIApplication
*)application {

// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

NSLog(@"程序意外退出");

NSLog(@"%s %d",
__FUNCTION__,__LINE__);

// 意外退出

}

- (void)applicationDidReceiveMemoryWarning:(UIApplication
*)application

{

NSLog(@"内存警告是会触发");
}
UIWindow
UIWindow作为应⽤程序的窗⼝,在应⽤程序启动的时候就要加载, 各种UI界⾯都是借助window来呈现的。
UIWindow在创建的时候,应该和屏幕⼀样⼤。通过[UIScreen mainScreen].bounds可以获得屏幕⼤⼩。
[self.window makeKeyAndVisible];//让window变的可视,即显⽰ window。
总结
UILabel、UITextField、UIButton都是特别常⽤的控件。
他们都是直接或者间接继承于UIView,只是外观和功能不同。
熟悉各种控件的特点和⽅法是我们后期做项⺫的保障。
delegate是iOS重要的设计模式,理解它的原理,有助于写出优质代码 main函数是程序的⼊⼝。
UIApplicationMain函数创建了应⽤程序实例,应 ⽤程序代理实例,建⽴事件循环。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: