zoukankan      html  css  js  c++  java
  • 自定义视图 视图控制器(UIViewController)

    CustomViewAndUIViewController

    loadView方法内部对self.view进行创建

    RootViewController继承于UIViewContrller的子类

    自定义视图

        1.设计控件布局

        2.找到底板, 以底板的类为父类, 创建一个子类

        3.针对底板上的控件, 依次写属性

        4.重写父类(UIView)的初始化方法, 在初始化方法内部对底板上的控件进行创建

        5.创建一个自定义视图类的对象, 验证自定义视图(布局, 样式)

     

    AppDelegate.m
     (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        
    RootViewController
    *rootVC = [[RootViewController alloc] init]; self.window.rootViewController = rootVC; [rootVC release]; return YES; }

    RootViewController.m

    - (void)loadView {
    //    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.view = [[LoginView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
          NSLog(@"加载视图");
    }
    视图已经加载完成
    - (void)viewDidLoad {
        NSLog(@"视图已经加载完成");
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor cyanColor];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
        label.text = @"周一见!";
        label.tag = 100;
        [self.view addSubview:label];
        [label release];
        
        self.view.backgroundColor = [UIColor colorWithRed:0.952 green:1.000 blue:0.456 alpha:1.000];
    
        LoginView *loginView = [[LoginView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        loginView.backgroundColor = [UIColor colorWithRed:0.957 green:1.000 blue:0.420 alpha:1.000];
        [self.view addSubview:loginView];
        [loginView release];
        
    }
    
    当应用的内存快不够用时, 就会发出警告, 系统会让当前显示的视图控制器执行didReceiveMemoryWarning
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
        NSLog(@"收到内存警告");
        UILabel *label = (UILabel *)[self.view viewWithTag:100];
       从父视图移除某个子视图
        [label removeFromSuperview];   
    }
    #import "HomeViewController.h"
    #define kSize 100
    @interface HomeViewController () {
        UILabel *label2;
        UILabel *label3;
        UILabel *label4;
    }
    
    @end
    @implementation HomeViewController
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.view.backgroundColor = [UIColor colorWithRed:0.998 green:0.704 blue:1.000 alpha:1.000];
        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kSize, kSize)];
        label1.backgroundColor = [UIColor yellowColor];
        label1.text = @"1";
        label1.font = [UIFont systemFontOfSize:80];
        label1.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:label1];
        [label1 release];   
        label2 = [[UILabel alloc] initWithFrame:CGRectMake(375 - kSize, 0, kSize, kSize)];
        label2.backgroundColor = [UIColor yellowColor];
        label2.text = @"2";
        label2.font = [UIFont systemFontOfSize:80];
        label2.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:label2];
        [label2 release];
        label3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 667 - kSize, kSize, kSize)];
        label3.backgroundColor = [UIColor yellowColor];
        label3.text = @"3";
        label3.font = [UIFont systemFontOfSize:80];
        label3.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:label3];
        [label3 release];
    
        label4 = [[UILabel alloc] initWithFrame:CGRectMake(375 - kSize, 667 - kSize, kSize, kSize)];
        label4.backgroundColor = [UIColor yellowColor];
        label4.backgroundColor = [UIColor yellowColor];
        label4.text = @"4";
        label4.font = [UIFont systemFontOfSize:80];
        label4.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:label4];
        [label4 release];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    UIViewController控制旋转的方法
    是否支持旋转, 默认YES
    - (BOOL)shouldAutorotate {
        return YES;
    }
    
    - (NSUInteger)supportedInterfaceOrientations {
            
            UIInterfaceOrientationMaskPortrait: 肖像模式
            UIInterfaceOrientationMaskLandscapeRight: 风景模式右
            UIInterfaceOrientationMaskPortraitUpsideDown: 肖像模式倒置
            UIInterfaceOrientationMaskLandscape: 风景模式(左, 右)
            UIInterfaceOrientationMaskAll: 四个方向
            UIInterfaceOrientationMaskAllButUpsideDown: 除了肖像模式倒置的所有方向
            
            iPhone项目 默认:  UIInterfaceOrientationMaskAllButUpsideDown
            iPad项目 默认: UIInterfaceOrientationMaskAll
        return UIInterfaceOrientationMaskAll;
    }
    
    视图将要旋转, 执行这个方法
    - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
        size, 当前视图的大小
        NSLog(@"%@", NSStringFromCGSize(size));
        if (size.width > size.height) {
            NSLog(@"横屏");
            view的frame不允许单独修改, 必须整体修改
            //1
            label2.frame = CGRectMake(667 - kSize, 0, kSize, kSize);
            //2
            CGRect rect = label2.frame;
            rect.origin.x = size.width - kSize;
            label2.frame = rect;
        } else {
            NSLog(@"竖屏");
           label2.frame = CGRectMake(size.width - kSize, 0, kSize, kSize);
           
        }
        
        CGRect rect = label2.frame;
        rect.origin.x = size.width - kSize;
        label2.frame = rect;
        
        rect = label3.frame;
        rect.origin.y = size.height - kSize;
        label3.frame = rect;
        
        rect = label4.frame;
        rect.origin.y = size.height - kSize;
        rect.origin.x = size.width - kSize;
        label4.frame = rect;      
    }

    封装Label - TextField界面

    LTView.h
    #import <UIKit/UIKit.h>
    @interface LTView : UIView
    @property (nonatomic, retain) UILabel *label;
    @property (nonatomic, retain) UITextField *textField;
    @end
    LTView.m
    #import "LTView.h"
    @implementation LTView
    - (void)dealloc {
        [_label release];
        [_textField release];
        [super dealloc];
    }
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            //label
            _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width / 3, frame.size.height)];
    //        _label.backgroundColor = [UIColor colorWithRed:0.426 green:1.000 blue:0.956 alpha:1.000];
            _label.textAlignment = NSTextAlignmentCenter;
            [self addSubview:_label];
            [_label release];        
            //textField
            _textField = [[UITextField alloc] initWithFrame:CGRectMake(_label.frame.size.width, 0, frame.size.width - _label.frame.size.width, frame.size.height)];//frame.size.width / 3 * 2
    //        _textField.backgroundColor = [UIColor whiteColor];
            [_textField setBorderStyle:(UITextBorderStyleRoundedRect)];
            [self addSubview:_textField];
            [_textField release];     
        }
        return self;
    }
    @end

    封装Login界面

    LoginView.h
    #import <UIKit/UIKit.h>
    @class LTView;
    @interface LoginView : UIView
    @property (nonatomic, retain) LTView *userNameView, *passwordView;
    @property (nonatomic, retain) UIButton *button;
    @end
    LoginView.m
    #import "LoginView.h"
    #import "LTView.h"
    @implementation LoginView
    - (void)dealloc {
        [_userNameView release];
        [_passwordView release];
        [_button release];
        [super dealloc];
    }
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            //用户名
            self.userNameView = [[LTView alloc] initWithFrame:CGRectMake(50, 100, 275, 40)];
            _userNameView.label.text = @"用 户 名";
            _userNameView.textField.placeholder = @" 请输入用户名";
            [self addSubview:_userNameView];
            [_userNameView release];
            //密码
            self.passwordView = [[LTView alloc] initWithFrame:CGRectMake(50, 160, 275, 40)];
            _passwordView.label.text = @"密      码";
            _passwordView.textField.placeholder = @" 请输入密码";
            _passwordView.textField.secureTextEntry = YES;
            [self addSubview:_passwordView];
            [_passwordView release];
            //登录按钮
            self.button = [UIButton buttonWithType:UIButtonTypeSystem];
            _button.frame = CGRectMake(100, 220, 375 - 200, 40);
    //        _button.backgroundColor = [UIColor greenColor];
    //        _button.showsTouchWhenHighlighted = YES;
            [_button setTitle:@"登录" forState:UIControlStateNormal];
            _button.titleLabel.font = [UIFont systemFontOfSize:20];
            [self addSubview:_button];
        }
        return self;
    }
    @end

     MVC: 是一种设计框架

        M: model, 数据模型, 用于定义数据结构, 存储数据

        V: view, 视图, 用于展示内容

        C: controller, 控制器, 作为modelview的协调者

        1.MVC,面向对象程序中出现的对象进行分类, 规定各自的作用

        2.modelview之间不能通信

        3.controller可以访问modelview, modelview不能访问controller

        4.view可以间接访问controller, 主要的通信手段

         a.target-action(目标动作机制)

         b.delegate(代理模式)

         c.dataSource(也是代理模式, 只不过换了一个名字, 主要用于数据的传递)

        5.moodel可以间接访问controller, 主要的通信手段

         a.KVO(键值观察)

         b.notification(通知)

        UIViewController, 视图控制器, 属于控制类, viewmodel的协调者, MVC框架的核心, 继承于UIResponder, 管理iOS应用中的view

        appDelegate中的视图创建的的代码, 转移到UIViewController

        RootViewController *rootVC = [[RootViewController alloc] init];

        指定window的根视图控制器

        self.window.rootViewController = rootVC;

        [rootVC release];

        : 视图控制器会自带一个UIView, 当指定window的根视图控制器后, 会出现window的上方, 并且和window的大小相同

     

    The one who wants to wear a crown must bear the weight!
  • 相关阅读:
    JS站点
    1011 World Cup Betting (20分)
    1007 Maximum Subsequence Sum (25分)(动态规划DP)
    1006 Sign In and Sign Out (25分)
    1005 Spell It Right (20分)
    1004 Counting Leaves (30分)(DFS)
    1003 Emergency (25分)(Dijkstra算法)
    1002 A+B for Polynomials (25分)
    1001 A+B Format (20分)
    canvas
  • 原文地址:https://www.cnblogs.com/OrangesChen/p/4895863.html
Copyright © 2011-2022 走看看