UIViewController : NSObject
视图控制器,管理UIView
MVC模式: Model-View-Controller(模型-视图-控制器)
Delegate:范围小,具体实现某一个功能的时候使用
实现代理的步骤:
1.声明一个协议(本质上是声明接口,可以是多个),协议中的接口不实现
2.被代理对象:定义一个成员变量,id<xxx协议> delegate 同时实现delegate的getter和setter方法
3.代理对象要遵循这个协议,并且协议的实现也在代理对象中
自定义UIViewController
@interface CustomUIViewController : UIViewController
@end
@implementation CustomUIViewController
@end
//UIViewController的执行流程
init->[super init]->initWithNibName ->init
self.window .rootViewController = controller -> loadView -> viewDidLoad
MyUIViewController *controller = [[MyUIViewController alloc]init];
self.view.rootViewController = controller;
[controller release];
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion NS_AVAILABLE_IOS(5_0);
//从一个UIViewController切换到一个新的UIViewController
UITextField : UIView
UITextField是继承自UIVIew的控件,作用是同用户进行交互,动态获取用户输入的信息
UITextField *field = [[UITextFieldalloc]initWithFrame:CGRectMake(0, 0, 240, 40)];
//设置UITextField的显示类型
[field setBorderStyle:UITextBorderStyleRoundedRect];
//设置field代理
//必须在声明文件中包含<UITextFieldDelegate>代理,否则会有警告
field.delegate = self;
// UITextField设置加密显示,默认值为NO
textField.secureTextEntry = YES;
在使用协议的时候,通常使用
#pragma mark -
#pragma mark 协议名或者….
表示将协议要实现的方法与类本身的方法分隔开来
/* 常用的几个个代理方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;//return NO to disallow editing.设置field是否可编辑,如果不实现该代理,默认返回YES.
- (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder ,第一响应者
- (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called,结束编辑
- (BOOL)textFieldShouldReturn:(UITextField *)textField;// called when 'return' key pressed. return NO to ignore.点击键盘return时调用,在使用该方法的时候,必须在定义UITextField控件的类的声明文件中加上<UITextFieldDelegate>协议的声明.
*/
[self.view addSubview:field];
[field release];
- (void)viewDidLoad; // Called after the view has been loaded. For view controllers created in code, this is after -loadView. For view controllers unarchived from a nib, this is after the view is set.
//viewDidLoad 在当前视图控制器加载后,不会再次调用,在UIViewcontroller之间进行切换时,会调用一下几个方法:
- (void)viewWillAppear:(BOOL)animated; // Called when the view is about to made visible. Default does nothing
- (void)viewDidAppear:(BOOL)animated; // Called when the view has been fully transitioned onto the screen. Default does nothing
- (void)viewWillDisappear:(BOOL)animated; // Called when the view is dismissed, covered or otherwise hidden. Default does nothing
- (void)viewDidDisappear:(BOOL)animated; // Called after the view was dismissed, covered or otherwise hidden. Default does nothing