UI - UIViewController

来源:互联网 发布:百度高级优化师 编辑:程序博客网 时间:2024/06/11 04:18

<AppDelegate.m>

#import "AppDelegate.h"#import "RootViewController.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];           //创建 RootViewController 对象,并设置为 window 的根视图控制器    self.window.rootViewController = [[RootViewController alloc] init];    [self.window.rootViewController release];        return YES;}- (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


<RootViewController.h>

#import <UIKit/UIKit.h>@interface RootViewController : UIViewController <UITextFieldDelegate>@end


<RootViewController.m>

#import "RootViewController.h"#import "LoginView.h"@interface RootViewController (){    LoginView *_loginView;}@end@implementation RootViewController//指定初始化方法- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization             }    return self;}/*//当视图控制器的 View 将要被使用时,该方法被系统自动调用//该方法完成了视图控制器的View 的创建//当有特殊需求需要自己创建视图控制器 view 时,重写该方法 , 注:不允许使用 super . 此方法是默认的,无特殊要求时不用重写.-(void)loadView{//    [super loadView];  //如果此行不被注释的话,则不能创建一个 View, 则此方法会循环执行,直到程序崩溃    //创建登陆界面    LoginView *loginView = [[LoginView alloc] init];    loginView.nameView.textField.delegate = self;    loginView.passWordView.textField.delegate = self;    self.view = loginView;    [loginView release];  //注:在 loadview 中添加的 view 初始化不用必须添加 frame 的初值,但是在 viewdidload 中是必须要添加 frame 的初值的,否则页面只能显示不能点击和输入 }*///当 loadView方法走完, view 创建之后立即调用- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.        self.view.backgroundColor = [UIColor redColor];        //创建登陆界面    _loginView = [[LoginView alloc] initWithFrame:self.view.bounds];    _loginView.nameView.textField.delegate = self;    _loginView.passWordView.textField.delegate = self;    [self.view addSubview:_loginView];    [_loginView release];                }//该方法在视图控制器的 view 将要显示的时候触发//主要进行对视图控制器的 view 以及其子视图进行重新布局//也就是说,只有在该方法中设置 view 的 frame 才是可行的-(void)viewWillLayoutSubviews{    [super viewWillLayoutSubviews];    self.view.frame = CGRectMake(10, 10, 300, 548);}//当收到内存警告时触发- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.    NSLog(@"@@@@@@@@@@内存警告@@@@@@@@@@");        //清除不需要的视图,满足以下两个条件    //1.视图已经被创建    //2.不需要在 window 上显示了        if ([self isViewLoaded] && !self.view.window) {        self.view = nil;//将视图清除    }    }//==================键盘回收=====================//-(BOOL)textFieldShouldReturn:(UITextField *)textField//{//    [textField resignFirstResponder];//    return YES;//}-(BOOL)textFieldShouldReturn:(UITextField *)textField{    //实现第一个 TextField 输入时点击 return 光标跳转到下一个 textField    if ([textField isEqual:_loginView.nameView.textField]) {   //isEqual: 也可以给 uitextfield 发送消息        //点击 return ,下一个 textField 成为第一响应者,则光标跳转到下一个 textField,        [_loginView.passWordView.textField becomeFirstResponder];            }else{                [textField resignFirstResponder];            }return YES;}//==================屏幕旋转=====================//屏幕旋转//设置是否支持旋转-(BOOL)shouldAutorotate{    return YES;}//设置支持旋转的方向-(NSUInteger)supportedInterfaceOrientations{   //  UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight; //允许左右旋转    return UIInterfaceOrientationMaskAll ;// 表示支持每个方向,但倒方向肯定是不会支持的.}//将要旋转时触发1-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{    NSLog(@"-----将要旋转时触发  %s   %d-----",__FUNCTION__,__LINE__);}//将要旋转时触发2-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{    NSLog(@"=====将要旋转时触发 %s   %d=====",__FUNCTION__,__LINE__);      //1.获取屏幕当前方向    UIInterfaceOrientation a = [UIApplication sharedApplication].statusBarOrientation;        //2.根据屏幕旋转方向进行 loginView 的重新布局    if (a == UIInterfaceOrientationLandscapeLeft || a == UIInterfaceOrientationLandscapeRight) {        _loginView.nameView.frame = CGRectMake(150, 40, 268, 40);        _loginView.passWordView.frame = CGRectMake(150, 110, 268, 40);        _loginView.loginBt.frame = CGRectMake(100, 180, 90, 40);        _loginView.registBt.frame = CGRectMake(240, 180, 90, 40);        _loginView.forgetBt.frame = CGRectMake(380, 180, 90, 40);    }else{        _loginView.nameView.frame = CGRectMake(20, 100, 280, 40);        _loginView.passWordView.frame = CGRectMake(20, 180, 280, 40);        _loginView.loginBt.frame = CGRectMake(10, 300, 90, 40);        _loginView.registBt.frame = CGRectMake(110, 300, 90, 40);        _loginView.forgetBt.frame = CGRectMake(220, 300, 90, 40);    }}//已经旋转触发-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{    NSLog(@"已经旋转时触发 %s   %d",__FUNCTION__,__LINE__);    }/*#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


<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