自定义视图 视图控制器(UIViewController)
CustomViewAndUIViewController
loadView方法內部對self.view進行創建
RootViewController繼承于UIViewContrller的子類
自定義視圖
? ? 1.設計控件布局
? ? 2.找到底板, 以底板的類為父類, 創建一個子類
? ? 3.針對底板上的控件, 依次寫屬性
? ? 4.重寫父類(UIView)的初始化方法, 在初始化方法內部對底板上的控件進行創建
? ? 5.創建一個自定義視圖類的對象, 驗證自定義視圖(布局, 樣式)
?
AppDelegate.m(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 *rootVC = [[RootViewController alloc] init];self.window.rootViewController = rootVC;[rootVC release]; return YES; }
RootViewController.m
- (void)loadView { // self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];self.view = [[LoginView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];NSLog(@"加載視圖"); } 視圖已經加載完成 - (void)viewDidLoad {NSLog(@"視圖已經加載完成");[super viewDidLoad]; self.view.backgroundColor = [UIColor cyanColor];UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; label.text = @"周一見!";label.tag = 100;[self.view addSubview:label];[label release];self.view.backgroundColor = [UIColor colorWithRed:0.952 green:1.000 blue:0.456 alpha:1.000]; LoginView *loginView = [[LoginView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; loginView.backgroundColor = [UIColor colorWithRed:0.957 green:1.000 blue:0.420 alpha:1.000]; [self.view addSubview:loginView]; [loginView release]; }當應用的內存快不夠用時, 就會發出警告, 系統會讓當前顯示的視圖控制器執行didReceiveMemoryWarning - (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.NSLog(@"收到內存警告");UILabel *label = (UILabel *)[self.view viewWithTag:100];從父視圖移除某個子視圖 [label removeFromSuperview]; } #import "HomeViewController.h" #define kSize 100 @interface HomeViewController () {UILabel *label2;UILabel *label3;UILabel *label4; }@end @implementation HomeViewController - (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.self.view.backgroundColor = [UIColor colorWithRed:0.998 green:0.704 blue:1.000 alpha:1.000];UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kSize, kSize)];label1.backgroundColor = [UIColor yellowColor];label1.text = @"1";label1.font = [UIFont systemFontOfSize:80];label1.textAlignment = NSTextAlignmentCenter;[self.view addSubview:label1];[label1 release]; label2 = [[UILabel alloc] initWithFrame:CGRectMake(375 - kSize, 0, kSize, kSize)];label2.backgroundColor = [UIColor yellowColor];label2.text = @"2";label2.font = [UIFont systemFontOfSize:80];label2.textAlignment = NSTextAlignmentCenter;[self.view addSubview:label2];[label2 release];label3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 667 - kSize, kSize, kSize)];label3.backgroundColor = [UIColor yellowColor];label3.text = @"3";label3.font = [UIFont systemFontOfSize:80];label3.textAlignment = NSTextAlignmentCenter;[self.view addSubview:label3];[label3 release];label4 = [[UILabel alloc] initWithFrame:CGRectMake(375 - kSize, 667 - kSize, kSize, kSize)];label4.backgroundColor = [UIColor yellowColor];label4.backgroundColor = [UIColor yellowColor];label4.text = @"4";label4.font = [UIFont systemFontOfSize:80];label4.textAlignment = NSTextAlignmentCenter;[self.view addSubview:label4];[label4 release]; }- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated. }UIViewController控制旋轉的方法 是否支持旋轉, 默認YES - (BOOL)shouldAutorotate {return YES; }- (NSUInteger)supportedInterfaceOrientations { UIInterfaceOrientationMaskPortrait: 肖像模式UIInterfaceOrientationMaskLandscapeRight: 風景模式右UIInterfaceOrientationMaskPortraitUpsideDown: 肖像模式倒置UIInterfaceOrientationMaskLandscape: 風景模式(左, 右)UIInterfaceOrientationMaskAll: 四個方向UIInterfaceOrientationMaskAllButUpsideDown: 除了肖像模式倒置的所有方向iPhone項目 默認: UIInterfaceOrientationMaskAllButUpsideDowniPad項目 默認: UIInterfaceOrientationMaskAllreturn UIInterfaceOrientationMaskAll; }視圖將要旋轉, 執行這個方法 - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {size, 當前視圖的大小NSLog(@"%@", NSStringFromCGSize(size));if (size.width > size.height) {NSLog(@"橫屏");view的frame不允許單獨修改, 必須整體修改//1 label2.frame = CGRectMake(667 - kSize, 0, kSize, kSize);//2 CGRect rect = label2.frame; rect.origin.x = size.width - kSize; label2.frame = rect;} else {NSLog(@"豎屏"); label2.frame = CGRectMake(size.width - kSize, 0, kSize, kSize); }CGRect rect = label2.frame;rect.origin.x = size.width - kSize;label2.frame = rect;rect = label3.frame;rect.origin.y = size.height - kSize;label3.frame = rect;rect = label4.frame;rect.origin.y = size.height - kSize;rect.origin.x = size.width - kSize;label4.frame = rect; }封裝Label - TextField界面
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 - (void)dealloc {[_label release];[_textField release];[super dealloc]; } - (instancetype)initWithFrame:(CGRect)frame {self = [super initWithFrame:frame];if (self) {//label_label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width / 3, frame.size.height)]; // _label.backgroundColor = [UIColor colorWithRed:0.426 green:1.000 blue:0.956 alpha:1.000];_label.textAlignment = NSTextAlignmentCenter;[self addSubview:_label];[_label release]; //textField_textField = [[UITextField alloc] initWithFrame:CGRectMake(_label.frame.size.width, 0, frame.size.width - _label.frame.size.width, frame.size.height)];//frame.size.width / 3 * 2 // _textField.backgroundColor = [UIColor whiteColor]; [_textField setBorderStyle:(UITextBorderStyleRoundedRect)];[self addSubview:_textField];[_textField release]; }return self; } @end封裝Login界面
LoginView.h #import <UIKit/UIKit.h> @class LTView; @interface LoginView : UIView @property (nonatomic, retain) LTView *userNameView, *passwordView; @property (nonatomic, retain) UIButton *button; @end LoginView.m #import "LoginView.h" #import "LTView.h" @implementation LoginView - (void)dealloc {[_userNameView release];[_passwordView release];[_button release];[super dealloc]; }- (instancetype)initWithFrame:(CGRect)frame {self = [super initWithFrame:frame];if (self) {//用戶名self.userNameView = [[LTView alloc] initWithFrame:CGRectMake(50, 100, 275, 40)];_userNameView.label.text = @"用 戶 名";_userNameView.textField.placeholder = @" 請輸入用戶名";[self addSubview:_userNameView];[_userNameView release];//密碼self.passwordView = [[LTView alloc] initWithFrame:CGRectMake(50, 160, 275, 40)];_passwordView.label.text = @"密 碼";_passwordView.textField.placeholder = @" 請輸入密碼";_passwordView.textField.secureTextEntry = YES;[self addSubview:_passwordView];[_passwordView release];//登錄按鈕self.button = [UIButton buttonWithType:UIButtonTypeSystem];_button.frame = CGRectMake(100, 220, 375 - 200, 40); // _button.backgroundColor = [UIColor greenColor]; // _button.showsTouchWhenHighlighted = YES;[_button setTitle:@"登錄" forState:UIControlStateNormal];_button.titleLabel.font = [UIFont systemFontOfSize:20];[self addSubview:_button];}return self; } @end?MVC: 是一種設計框架
? ? M: model, 數據模型, 用于定義數據結構, 存儲數據
? ? V: view, 視圖, 用于展示內容
? ? C: controller, 控制器, 作為model和view的協調者
? ? 1.MVC把,面向對象程序中出現的對象進行分類, 規定各自的作用
? ? 2.model和view之間不能通信
? ? 3.controller可以訪問model和view, model和view不能訪問controller
? ? 4.view可以間接訪問controller, 主要的通信手段
?? ? a.target-action(目標動作機制)
?? ? b.delegate(代理模式)
?? ? c.dataSource(也是代理模式, 只不過換了一個名字, 主要用于數據的傳遞)
? ? 5.moodel可以間接訪問controller, 主要的通信手段
?? ? a.KVO(鍵值觀察)
?? ? b.notification(通知)
? ? UIViewController, 視圖控制器, 屬于控制類, view和model的協調者, 是MVC框架的核心, 繼承于UIResponder, 管理iOS應用中的view
? ? 把appDelegate中的視圖創建的的代碼, 轉移到UIViewController中
? ? RootViewController *rootVC = [[RootViewController alloc] init];
? ? 指定window的根視圖控制器
? ? self.window.rootViewController = rootVC;
? ? [rootVC release];
? ? 注: 視圖控制器會自帶一個UIView, 當指定window的根視圖控制器后, 會出現window的上方, 并且和window的大小相同
?
轉載于:https://www.cnblogs.com/OrangesChen/p/4895863.html
總結
以上是生活随笔為你收集整理的自定义视图 视图控制器(UIViewController)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Fiddler学习之——对Android
- 下一篇: 笔记本苹果键盘失灵怎么办win7 win