UI - 简单封装视图

来源:互联网 发布:上海市物业大楼数据 编辑:程序博客网 时间:2024/05/22 17:38

<AppDelegate.h>

#import <UIKit/UIKit.h>@interface AppDelegate : UIResponder <UIApplicationDelegate,UITextFieldDelegate>@property (strong, nonatomic) UIWindow *window;@end


<AppDelegate.m>

#import "AppDelegate.h"#import "LTView.h"#import "LoginView.h"@implementation AppDelegate- (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];            //========================登陆界面========================            //创建背景视图    UIView *containterView = [[UIView alloc] initWithFrame:self.window.bounds];    containterView.backgroundColor = [UIColor whiteColor];    [self.window addSubview:containterView];    [containterView release];    //    //用户//    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(20, 100, 90, 50)];//    name. text = @"用户名:";//    [containterView addSubview:name];//    [name release];//    //    UITextField *inputName = [[UITextField alloc] initWithFrame:CGRectMake(130, 100, 170, 50)];//    inputName.placeholder = @"请输入用户名";//    inputName.borderStyle = UITextBorderStyleRoundedRect;//    inputName.tag = 101;//    inputName.delegate = self;//    [containterView addSubview:inputName];//    [inputName release];//    //    //密码//    UILabel *passWord = [[UILabel alloc ] initWithFrame:CGRectMake(20, 170, 90, 50)];//    passWord.text = @"密码:";//    [containterView addSubview:passWord];//    [passWord release];//    //    UITextField *inputPassWord = [[UITextField alloc] initWithFrame:CGRectMake(130, 170, 170, 50)];//    inputPassWord.placeholder = @"请输入密码";//    inputPassWord.borderStyle = UITextBorderStyleRoundedRect;//    inputPassWord.tag = 102;//    inputPassWord.delegate = self;//    [containterView addSubview:inputPassWord];//    [inputPassWord release];//    //    //按钮//    UIButton *login = [UIButton buttonWithType:UIButtonTypeSystem];//    login.frame = CGRectMake(10, 260, 90, 40);//    [login setTitle:@"登陆" forState: UIControlStateNormal];//    [login addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];//    [containterView addSubview:login];//    //    UIButton *cancle = [UIButton buttonWithType:UIButtonTypeSystem];//    cancle.frame = CGRectMake(110, 260, 90, 40);//    [cancle setTitle:@"取消" forState: UIControlStateNormal];//    [cancle addTarget:self action:@selector(cancle) forControlEvents:UIControlEventTouchUpInside];//    [containterView addSubview:cancle];//    //    UIButton *find = [UIButton buttonWithType:UIButtonTypeSystem];//    find.frame = CGRectMake(210, 260, 90, 40);//    [find setTitle:@"找回密码" forState: UIControlStateNormal];//    [find addTarget:self action:@selector(find) forControlEvents:UIControlEventTouchUpInside];//    [containterView addSubview:find];            //========================自定义label-textField视图的登陆界面========================            //LTView 的使用    //用户    LTView *userName = [[LTView alloc] initWithFrame:CGRectMake(20, 100, 280, 40)];    userName.label.text = @"用户名";    userName.textField.placeholder = @"请输入用户名";    userName.textField.delegate = self;        [containterView addSubview:userName];    [userName release];            //密码    LTView *passWord = [[LTView alloc] initWithFrame:CGRectMake(20, 200, 280, 40)];    passWord.label.text = @"密码";    passWord.textField.placeholder = @"请输入密码";    passWord.textField.delegate = self;    [containterView addSubview:passWord];    [passWord release];                    //========================自定义LTView-UIButton视图的登陆界面========================            //创建 LoginView    LoginView *loginView = [[LoginView alloc]initWithFrame:self.window.bounds];    loginView.backgroundColor  = [UIColor lightGrayColor];    loginView.nameView.textField.delegate = self;    loginView.passWordView.textField.delegate = self;    [self.window addSubview:loginView];    [loginView release];              return YES;}-(BOOL)textFieldShouldReturn:(UITextField *)textField{    [textField resignFirstResponder];    return YES;}-(void)login{        NSLog(@"登陆");}-(void)find{    NSLog(@"找回密码");}-(void)cancle{    NSLog(@"取消") ;}- (void)applicationWillResignActive:(UIApplication *)application{    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application{    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}- (void)applicationWillEnterForeground:(UIApplication *)application{    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application{    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application{    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end


<LoginView.h>

#import <UIKit/UIKit.h>#import "LTView.h"@interface LoginView : UIView@property (nonatomic,retain)LTView *nameView;  //用户@property (nonatomic,retain)LTView *passWordView;  //密码@property (nonatomic,retain)UIButton *loginBt;    //登陆按钮@property (nonatomic,retain)UIButton *registBt;   //注册按钮@property (nonatomic,retain)UIButton *forgetBt;   //忘记密码按钮@end


<LoginView.m>

#import "LoginView.h"@implementation LoginView- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        // Initialization code                //用户        self.nameView = [[LTView alloc ] initWithFrame:CGRectMake(20, 100, 280, 40)];        _nameView.label.text = @"用户名:";        _nameView.textField.placeholder = @"请输入用户名";        [self addSubview:_nameView];        [_nameView release];                //密码        self.passWordView = [[LTView alloc] initWithFrame:CGRectMake(20, 180, 280, 40)];        _passWordView.label.text = @"密码:";        _passWordView.textField.placeholder = @"请输入密码";        [self addSubview:_passWordView];        [_passWordView release];                //按钮        self.loginBt = [UIButton buttonWithType:UIButtonTypeSystem];        _loginBt.frame = CGRectMake(10, 300, 90, 40);        [_loginBt setTitle:@"登陆" forState: UIControlStateNormal];        [self addSubview:_loginBt];                self.registBt = [UIButton buttonWithType:UIButtonTypeSystem];        _registBt.frame = CGRectMake(110, 300, 90, 40);        [_registBt setTitle:@"注册" forState: UIControlStateNormal];        [self addSubview:_registBt];                self.forgetBt = [UIButton buttonWithType:UIButtonTypeSystem];        _forgetBt.frame = CGRectMake(220, 300, 90, 40);        [_forgetBt setTitle:@"忘记密码" forState: UIControlStateNormal];        [self addSubview:_forgetBt];                    }    return self;}-(void)dealloc{    self.nameView = nil;    self.passWordView = nil;    self.loginBt = nil;    self.registBt = nil;    self.forgetBt = nil;    [super dealloc];}/*// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect{    // Drawing code}*/@end


<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- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        // Initialization code                //1.获取LTView 高度和宽度        CGFloat width = self.bounds.size.width;        CGFloat heigh = self.bounds.size.height;                //2.计算 label 和 tf 的宽度和高度        CGFloat lbWidth = (width - 10) / 3 - 10;        CGFloat lbHight = (heigh - 10);                CGFloat tfWidth = (width - 10) / 3 * 2 - 10;        CGFloat tfHight = (heigh - 10);                //创建 label 和 textField        //创建 label 对象  并给属性赋值        self.label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, lbWidth, lbHight)];        //居中        _label.textAlignment = NSTextAlignmentCenter;        [self addSubview:_label];        [_label release];                        self.textField = [[UITextField alloc] initWithFrame:CGRectMake(5 + lbWidth + 10, 5, tfWidth,tfHight)];        _textField.backgroundColor = [UIColor whiteColor];        _textField.borderStyle = UITextBorderStyleRoundedRect;        [self addSubview:_textField];        [_textField release];            }    return self;}-(void)dealloc{    self.label = nil;    self.textField = nil;    [super dealloc];}/*// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect{    // Drawing code}*/@end


0 0
原创粉丝点击