概述:
UIViewController:视图控制器。
控制视图显⽰示,响应事件。
分担AppDelegate的⼯工作。
实现模块独⽴立,提⾼高复⽤用性
1.作用:
a.创建和管理视图。
b.管理视图上显示的数据
c.设备方向变化,调整视图大小以适应屏幕
d.负责视图和模型之间的数据及请示的传递
2.视图控制器剖析
视图控制器有一个导航项,一个工具栏子项集以及一个tabbarItem项与其关联。
3.主要属性
view:控制器管理的根视图
title:控制器导航栏的标题
wantsFullScreenLayout:是否需要全屏显示
interfaceOrientation:界面方向
navigationItem:导航子项
editing:是否处于编辑状态
hidesBonttomBarWhenPushed:入栈时隐藏底部栏
toolbarItems:工具栏子项集
tabBarItem: 标签栏子项
editButtonItem:返回一个编辑按钮
parentViewController:交视图控制器
searchDisplayController:搜索显示控制器
splitViewController:分割视图控制器
modalViewController:模式控制器
navigationController:导航控制器
tabBarController:标签控制器
4主要方法
-loadView:不要主动调用该方法,当访问控制器view的时候,就会调用该方法。如果要自己创建view,则需要重写该方法。
-viewDidload:当加载控制器的视图到内存时,该方法被调用
-viewWillAppear:视图将要出现
-viewDidAppear:视图已经出现
-viewWillDisappear:视图将要消失
-viewDidDisappear:视图已经消失
-shouldAutorotateToInterfaceOrientation:是否支持指定的界面
-willAnimateRotationToInterfaceOrientation : duration:将要使用动画过渡到某个界面方向
-didReceiveMemoryWarning:接收到内存警告消息。
-presentModalViewController:animated:显示模式控制器
-dismissModalViewControllerAnimated:隐藏模式控制器
-setToolbarItems:animated:设置工具栏子项
-setEditing:animated:设置编辑状态
设计模式:两种MVC设计模式
1.传统的:
!传统模式
2.iOS设计模式
!iOS模式
代码(建一个继承于UIView的类LoginView)
//LoginView.h
#import<UIKit/UIKit.h>
#pragma mark - 声明两个属性
@property(nonatomic,strong) UITextField *textField;
@property(nonatomic,strong) UIButton *button;
@end
//LoginView.m
#import "LoginView.h"
@implementation LoginView
-(instancetype) initWithFrame:(CGRect)frame
{
if(self = [super initWithFrame:frame])
{
//添加输入框
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100,100,200,30)];
self.textField.borderStyle = UITextBorderStyleRoundedRect;
self.textField.borderStyle = UITextBorderStyleRoundedRect;
self.textField.placeholder = @"请输入";//占位符的作用
[self addSubview:self.textField];
//添加按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100,200,200,30);
[button setTitle:@"登录" forState:UIControlStateNormal];
[self addSubview:button];
self.button = button;
}
return self;
}
@end
创建一个继承于UIViewController的类
//LoginController.m
#import "LoginController.h"
#import "LoginView.h"
@interface LoginController()<UITextFieldDelegate>
#pragma mark - 声明私有属性
@property(nonatomic,strong) loginView *loginView;
@end
@implementation LoginController
#pragma mark - 使用自定义视图替换自带的view
- (void)loadView
{
self.loginView = [[LoginView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view = self.loginView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor ligthGrayColor];
//绑定按钮事件
[self.loginView.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
self.loginView.textField.delegate = self;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void) buttonAction:(UIButton *)sender
{
}
//AppDelegate.m
#import "AppDelegate.h"
#import "RootViewController.h"
#import "LoginController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
// //设置根视图控制器
// RootViewController *rootVC = [[RootViewController alloc]init];
// self.window.rootViewController = rootVC;
LoginController *loginVC = [LoginController new];
self.window.rootViewController = loginVC;
return YES;
}