RootViewController
//視圖控制器(UIViewController):它不是視圖,用來管理視圖,所以屏幕上看不到,但是自身攜帶一個視圖(根視圖) #import "RootViewController.h" #import "LoginView.h" //視圖控制器的延展 @interface RootViewController () @end //視圖控制器的實現部分 @implementation RootViewController //用來加載視圖控制器的根視圖,此方法源於父類 - (void)loadView { LoginView *logview = [[LoginView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; //指定剛才創建的 自定義視圖 對象為當前視圖控制器的根視圖 self.view = logview; [logview release]; } //該方法是在loadView執行完之后立即執行的方法 //當訪問視圖控制器的View為空,一但發現View為空,立即調用loadView來加載根視圖 //如果要自定義視圖控制器的根視圖,只需要重寫loadView方法,在loadView方法中完成創建,並指定當前試圖控制器的根視圖即可 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. NSLog(@"%s,%d",__FUNCTION__,__LINE__); // self.view.backgroundColor = [UIColor redColor]; } #pragma mark -- 檢測和處理屏幕的旋轉 //1.設置屏幕支持的旋轉方向 - (NSUInteger)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskAll; } //開始旋轉時會觸發的方法 //經常用來暫停播放器播放,暫停視屏播放,以及關閉用戶交互 - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; } //當旋轉結束是時觸發 //繼續音樂,視頻播放,以及打開的用戶交互 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ [super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; } //當將要開始旋轉做動畫時觸發,經常用來在旋轉時添加自定義動畫 - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration]; } #pragma ---視圖控制器控制布局視圖的方法 //當視圖旋轉時,視圖控制器觸發的方法,用於重新布局視圖控制器根據視圖上得子視圖 - (void)viewWillLayoutSubviews{ [super viewWillLayoutSubviews]; } //當視圖控制器收到內存警告時觸發 //釋放之前未使用的空間,以及可以重建的對象. - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. //1.視圖控制器的根視圖是夠已經成功加載 //2.當前根視圖是否正在顯示 //[self isViewLoaded]判斷視圖是否成功加載 //self.view.window,判讀視圖是否在當前界面 //內存加載成功,且不在當前界面 if ([self isViewLoaded] && !self.view.window) { [self.view release]; } } @end
LoginView.h
#import <UIKit/UIKit.h> @interface LoginView : UIView //最簡單的自定義視圖封裝:直接將控件聲明為屬性,在.h中聲明外界訪問的接口. @property (nonatomic,retain) UILabel *aLabel; @property (nonatomic, retain)UITextField *textField; @property (nonatomic, retain)UIButton *loginBtn; @end
LoginView.m
#import "LoginView.h" #define kMargin_Left_Label 30 #define kMargin_Top_Label 100 #define kWidth_Label 60 #define kHeight_Label 30 #define kMargin_Left_TextField (kMargin_Left_Label + kWidth_Label + 20) #define kMargin_Top_TextField 100 #define kWidth_TextField 150 #define kHeight_TextField 30 #define KMargin_Left_Button 100 #define kmargin_Top_Button (kMargin_Top_Label + kHeight_Label + 30) #define kWidth_Button 80 #define kHeight_Button 40 @implementation LoginView //原則:如果重寫父類繼承來方法是,如果不知道父類對該方法的事項,則調用父類對該方法的實現(super). - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if(self) { [self setupLabel]; [self setupButton]; [self setupTextField]; } return self; } //用戶名Label - (void)setupLabel { self.aLabel = [[UILabel alloc] init]; self.aLabel.text = @"用戶名"; self.aLabel.frame = CGRectMake(kMargin_Left_Label,kMargin_Top_Label , kWidth_Label, kHeight_Label); [self addSubview:self.aLabel]; [self.aLabel release]; } //輸入框 - (void)setupTextField{ self.textField = [[UITextField alloc] init]; self.textField.placeholder = @"請輸入用戶名"; self.textField.borderStyle = UITextBorderStyleRoundedRect; self.textField.frame = CGRectMake(kMargin_Left_TextField, kMargin_Top_TextField, kWidth_TextField, kHeight_TextField); [self addSubview:self.textField]; [self.textField release]; } //登陸按鈕 - (void)setupButton { self.loginBtn = [UIButton buttonWithType:UIButtonTypeSystem]; [self.loginBtn setTitle:@"登陸" forState:UIControlStateNormal]; self.loginBtn.frame = CGRectMake(KMargin_Left_Button, kmargin_Top_Button, kWidth_Button, kHeight_Button); [self addSubview:self.loginBtn]; } //該方法為視圖的方法,當旋轉時,視圖會自動的調用該方法用來重新布局子視圖. - (void)layoutSubviews{ [super layoutSubviews]; //1.獲取當前屏幕所處的狀態(旋轉方向) UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; //2.判斷屏幕方向,調整子視圖 switch (orientation) { case UIInterfaceOrientationPortrait: // NSLog(@"豎屏"); //break; case UIInterfaceOrientationPortraitUpsideDown: // NSLog(@"倒置"); self.loginBtn.frame = CGRectMake(KMargin_Left_Button, kmargin_Top_Button, kWidth_Button, kHeight_Button); break; case UIInterfaceOrientationLandscapeLeft: // NSLog(@"右橫屏"); //break; case UIInterfaceOrientationLandscapeRight: // NSLog(@"左橫屏"); self.loginBtn.frame = CGRectMake(kMargin_Left_TextField + kWidth_TextField + 30, kMargin_Top_Label, kWidth_Button, kHeight_Button); break; default: break; } } - (void)dealloc { [_aLabel release]; [_textField release]; [_loginBtn release]; [super dealloc]; } @end