UI -视图控制器UIViewController

视图主要负责搭建一个用来展示的框架,在我看来只需要在视图上设置各个控件的frame就可以了,其他操作都可以用视图控制器UIViewController来实现
下面是具体步骤
1>创建一个继承于UIView的视图

(#GetPasswordView.h)
#import <UIKit/UIKit.h>

@interface GetPasswordView : UIView
@property(nonatomic,retain,readonly)UITextField *textField; // 找回密码输入框
@property(nonatomic,readonly,retain)UIButton *getBackButton; // 找回按钮
@end

(#GetPasswordView.m)
#import "GetPasswordView.h"

@implementation GetPasswordView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)dealloc
{
    [_textField release];
    [_getBackButton release];
    [super dealloc];
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _textField = [[UITextField alloc]initWithFrame:CGRectMake(30, 80, 300, 30)];
        _textField.borderStyle = UITextBorderStyleRoundedRect;
        [self addSubview:_textField];

        _getBackButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
        _getBackButton.frame = CGRectMake(50, 150, 80, 30);
        [_getBackButton setTitle:@"找回" forState:(UIControlStateNormal)];
        [self addSubview:_getBackButton];
    }
    return self;
}
/*
// 当自身的bounds发生改变时触发该方法
- (void)layoutSubviews
{
    // 根据屏幕方向 布局子视图
    //1>获取当前屏幕方向
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
        // 布局横屏状态下的子视图
        <#statements#>
    }else{
        // 布局竖屏状态下的子视图
    }
}
*/
@end

2>创建一个继承于UIViewController的视图控制器

(#RootViewController.m)
#import "RootViewController.h"
#import "GetPasswordView.h"
@interface RootViewController ()

@end

@implementation RootViewController
// 1.这四个方法执行多少次
// 2.执行顺序
//- (void)view(Will/Did)Appear:(BOOL)animated
//- (void)view(Will/Did)Disappear:(BOOL)animated


/**
   ViewController:视图控制器.任何一个应用程序至少有一个根视图控制器.
   一般情况下,不会直接实例化ViewController 而是创建子类 然后实例化对象
   每一个视图控制器都自带一个View 这里采用的是懒加载的设计模式. 即只有当不得不需要View的时候 才会加载View
   当系统自带的View不满足用户需求的时候,可以在viewController的内部重写loadView放法 将自定义的视图指定为Contr的View
   loadView方法只会执行一次
   ViewDidload方法是在视图加载完之后才会执行的方法 一般情况下会在viewDidload方法中对view做一些相应的设置(设置背景颜色,添加事件,设置代理等等)viewDidload方法也是只会执行一次.
 */


/**
 *  loadView : 加载视图. 当第一次需要视图控制器的View的时候,会执行loadView方法.
    1.何时重写loadView方法?
    2.重写loadView方法时 应该注意什么?
 不要在loadView方法中调用View得getter方法,否则会产生死循环
 */
- (void)loadView
{
    /*
    UIView *view = [[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    view.backgroundColor = [UIColor lightGrayColor];
    //1.
//    [self.view addSubview:view];
    //2.

     self.view不能为nil,因此需要调用[super  loadView]方法,后者不需要显然,继承UIViewController
     的subclass要实现其load方法时,若采用添加view的方式,我们只要在loadView中增加一句[super 
     loadView]就没有问题了。但这并不是Cocoa的设计者所期望的。self.view = …才是提倡的方式

    self.view = view;
    [view release];
     */
    GetPasswordView *passwordView = [[GetPasswordView alloc]initWithFrame:[[UIScreen mainScreen]bounds]] ;
    self.view = passwordView;
    [passwordView release];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    // 给getBackButton添加点击事件.
    // 1.如何查找getBackButton
    ((UITextField *)self.view.subviews[0]).placeholder = @"电子邮箱";
    [((GetPasswordView *)self.view).getBackButton addTarget:self action:@selector(didClickGetBackButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    // 2.添加事件

    // 点击键盘上的return按钮时 回收键盘
    ((UITextField *)self.view.subviews[0]).delegate = self;

}
#pragma mark  - 点击找回密码
- (void)didClickGetBackButtonAction:(UIButton *)button
{

}
#pragma mark  - 点击return按钮,键盘回收
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
#pragma mark  - 设置设备支持的屏幕旋转方向
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    NSLog(@"内存警告了,赶紧下课吧");
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

3>在AppDelegate.m里设置根控制器

(#AppDelegate.m)
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate
- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];

    // 1.创建RootViewControl对象
    RootViewController *rootVC = [[RootViewController alloc]init];
    // 2.设置为windw的根视图控制器
    self.window.rootViewController = rootVC;
//    rootVC.view.backgroundColor = [UIColor blackColor];
    // 3.释放
    [rootVC release];

    [self.window makeKeyAndVisible];

    return YES;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值