zoukankan      html  css  js  c++  java
  • UI基础 自定义视图

    RootViewControlle.m
    #import "RootViewController.h"
    #import "MyView.h"
    @interface RootViewController ()
    
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // 第一步 新建一个类继承UIView
    //     第二步 分析新视图由哪一些旧视图构成 并且把它声明称属性
    //    第三步 重写初始化方法 在方法里面完成布局
        // 使用自定义视图
        MyView* view=[[MyView alloc] initWithFrame:CGRectMake(10, 100, 306, 50)];
        [self.view addSubview:view];
        
    //    view.label.text=@"用户名";
    //    view.tf.placeholder=@"请输入用户名";
    //
        
        
        [view setLabelText:@"hah"];
        
    }
    
    @end

    MyView.h

    #import <UIKit/UIKit.h>
    
    
    
    @interface MyView : UIView
    //声明一个方法 专门设置label的文字
    -(void)setLabelText:(NSString *)string;
    
    @end

    MyView.m

    #import "MyView.h"
    
    //延展
    @interface MyView()
    @property(nonatomic,strong)UILabel* label;
    @property(nonatomic,strong)UITextField* tf;
    
    @end
    
    
    @implementation MyView
    //重写初始化方法 完成布局
    -(id)initWithFrame:(CGRect)frame{
        self=[super initWithFrame:frame];
        if(self){
            
            _label=[[UILabel alloc]initWithFrame:CGRectMake(5, 5, 80, 40)];
            _tf=[[UITextField alloc] initWithFrame:CGRectMake(90, 5, 240, 40)];
            
            _label.backgroundColor=[UIColor purpleColor];
            _tf.backgroundColor=[UIColor redColor];
            self.backgroundColor=[UIColor cyanColor];
            
            
            [self addSubview:_label];
            [self addSubview:_tf];
            
            
        }
        return self;
        }
    
    //实现方法
    -(void)setLabelText:(NSString *)string
    {
        _label.text=string;
        
    }
    
    
    
    @end
  • 相关阅读:
    datagrid column width in silverlight.
    Dir你肿么了?是肿了么?越来越大?
    tut12几个简单的帧控制
    CreateThread(), _beginthread()和_beginthreadex()
    windows进程优先级
    Lingo06 Handler
    Lingo13 和Flash交互之getURL
    关于define活生生的SB实例,挂起来
    几个Director/Lingo站
    Lingo02 List
  • 原文地址:https://www.cnblogs.com/zhangqing979797/p/13357898.html
Copyright © 2011-2022 走看看