#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//--------------------------------------------------------------------------
RootViewController *rootViewController = [[RootViewController alloc]init];
self.window.rootViewController = rootViewController;
//--------------------------------------------------------------------------
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
#import "RootViewController.h"
#import "loginView.h"
#import "LTView.h"
#import "TwoViewController.h"
@interface RootViewController ()<UITextFieldDelegate>
@end
@implementation RootViewController
//記載視圖的方法 此方法一般不需要重寫,在此方法中可以定製當前視圖控制器的視圖
-(void)loadView{
[super loadView];
//先初始化登錄界面
loginView *loginView1 = [[loginView alloc]initWithFrame:[UIScreen mainScreen].bounds];
//調用屬性的getter方法,使登錄界面的子視圖初始化並加載到登錄界面上
loginView1.userNameLTView.rightTextField.delegate = self;
loginView1.pwdLTView.rightTextField.delegate = self;
//將登錄界面賦給self.view
self.view = loginView1;
}
#pragma mark -textField的代理方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
//回收鍵盤
[textField resignFirstResponder];
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
//添加按鈕,實現:點擊按鈕可以跳轉到TwoVC
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
[btn setFrame:CGRectMake(100, 300, 100, 100)];
[btn setTitle:@"跳轉" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(jumpAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
NSLog(@"登錄界面加載完成---%s",__FUNCTION__);
}
-(void)jumpAction:(UIButton *)sender{
TwoViewController *twoVC = [[TwoViewController alloc]init];
[self presentViewController:twoVC animated:YES completion:^{
}];
}
//內存警告,系統爲每個應用都分配了運行內存,一般情況下爲30M;當執行此方法的時候,說明當前應用程序內存已經超出了系統所分配的內存大小。所以要再此方法中進行一些資源的釋放。
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
//當內存警告時,將當前界面釋放掉
//首先需要判斷當前界面已經加載並且沒有顯示,這個時候纔可以釋放掉此界面。
//判斷當前控制器視圖是否加載 isViewLoaded
//如果值爲nil說明未顯示,如果不爲nil說明正在顯示
if ([self isViewLoaded] == YES && self.view.window ==nil) {
//對象指向nil的時候,只是對原來開闢的空間失去了所有權。也就是告訴系統這塊內存現在無人使用,當需要的時候,可以將此內存回收。
self.view = nil;
}
NSLog(@"釋放了登錄界面---%s",__func__);
}
//是否讓當前視圖支持屏幕旋轉 返回值爲YES:支持; NO:不支持
#pragma mark -屏幕旋轉相關代碼
-(BOOL)shouldAutorotate{
return YES;
}
//設置旋轉的方向
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAll;
// UIInterfaceOrientationMaskPortrait; 正常
// UIInterfaceOrientationLandscapeLeft; 向左
// UIInterfaceOrientationLandscapeRight;向右
// UIInterfaceOrientationMaskAll; 全部支持
//在枚舉值中,如果想要同時支持兩個枚舉類型,只需要用單或連接兩個枚舉值即可。
}
@end
#import "TwoViewController.h"
@interface TwoViewController ()
@end
@implementation TwoViewController
- (void)viewDidLoad {
[super viewDidLoad];
//--------------------------------------------------------------------------
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeSystem];
[backBtn setTitle:@"返回" forState:UIControlStateNormal];
backBtn.frame = CGRectMake(100, 100, 100, 100);
[backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backBtn];
NSLog(@"註冊界面加載完成---%s",__FUNCTION__);
//--------------------------------------------------------------------------
}
//返回上一界面
-(void)backAction:(UIButton *)sender{
[self dismissViewControllerAnimated:YES completion:^{
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
if ([self isViewLoaded] == YES && self.view.window ==nil) {
self.view = nil;
}
NSLog(@"哈哈%s",__func__);
}
#import "loginView.h"
#import "LTView.h"
@implementation loginView
//懶加載 實際上就是對屬性的getter方法的重寫,所以不需要手動調用此方法,屬性的點語法自然會調用自己的get方法。
-(LTView *)userNameLTView{
//每次調用getter方法的時候,都判斷屬性是否存在,如果存在,說明已經初始化過了,如果不存在說明沒有初始化,需要初始化。
if (!_userNameLTView) { //如果對象不存在
//當對象不存在的時候,就需要初始化對象
_userNameLTView = [[LTView alloc]initWithFrame:CGRectMake(40, 100, 280, 40)];
_userNameLTView.leftLabel.text = @"用戶名";
_userNameLTView.rightTextField.placeholder = @"請輸入用戶名";
[self addSubview:_userNameLTView];
}
return _userNameLTView;
}
-(LTView *)pwdLTView{
if (!_pwdLTView) {
_pwdLTView = [[LTView alloc]initWithFrame:CGRectMake(40, 160, 280, 40)];
_pwdLTView.leftLabel.text = @"密碼";
_pwdLTView.rightTextField.placeholder = @"請輸入密碼";
[self addSubview:_pwdLTView];
}
return _pwdLTView;
}
//當屏幕旋轉的時候,系統會調用此方法重新佈局,如果我們對系統的佈局不滿意,就需要重寫此方法,我們自己手動佈局
-(void)layoutSubviews{
[super layoutSubviews];
//要根據屏幕方向重新佈局,需要知道當前的屏幕方向
//得到當前的屏幕方向
//UIApplication sharedApplication 得到當前的應用程序
//statusBarOrientation 狀態欄的方向
NSUInteger orienation = [UIApplication sharedApplication].statusBarOrientation;
switch (orienation) {
//向左或者向右作爲同一種佈局處理,該界面上面的子控件位置居中,其實就是改變控件的X值
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:{
//首先得到整個屏幕的寬度,根據屏幕寬度讓子視圖水平居中
float screenWidth = [UIScreen mainScreen].bounds.size.width;
CGRect userNameFrame = self.userNameLTView.frame;
//居中其實就是 (屏幕的寬度-控件的寬度)/2
//得到控件的寬度
float subViewWidth =CGRectGetWidth(self.userNameLTView.frame);
//計算控件新的X值
userNameFrame.origin.x = (screenWidth - subViewWidth)/2;
self.userNameLTView.frame = userNameFrame;
}
case UIInterfaceOrientationPortrait:{
self.userNameLTView.frame = CGRectMake(100, 100, 280, 40);
self.pwdLTView.frame = CGRectMake(100, 160, 280, 40);
}
break;
default:
break;
}
}
#import "LTView.h"
@implementation LTView
//因爲labrl和TextField要根據LTView的大小來佈局,所以我們需要拿到LTView的frame,所以需要重寫LTView的初始化方法。
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
//在初始化方法中,爲LTView添加label 和 textField
//初始化label。並且添加到LTView。 label 和 textField的寬度比例大約爲1:2
//得到整個屏幕寬度
float sumWidth = CGRectGetWidth(frame);
//得到label的寬度
float labelWidth = (sumWidth-20)/3;
_leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, labelWidth, CGRectGetHeight(frame))];
[self addSubview:_leftLabel];
//初始化textField 並添加到LTView
_rightTextField =[[UITextField alloc]initWithFrame:CGRectMake(labelWidth + 20, 0,labelWidth *2, CGRectGetHeight(frame))];
[self addSubview:_rightTextField];
}
return self;
}