日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

自定义视图 视图控制器(UIViewController)

發(fā)布時(shí)間:2024/10/12 编程问答 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义视图 视图控制器(UIViewController) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

CustomViewAndUIViewController

loadView方法內(nèi)部對self.view進(jìn)行創(chuàng)建

RootViewController繼承于UIViewContrller的子類

自定義視圖

? ? 1.設(shè)計(jì)控件布局

? ? 2.找到底板, 以底板的類為父類, 創(chuàng)建一個(gè)子類

? ? 3.針對底板上的控件, 依次寫屬性

? ? 4.重寫父類(UIView)的初始化方法, 在初始化方法內(nèi)部對底板上的控件進(jìn)行創(chuàng)建

? ? 5.創(chuàng)建一個(gè)自定義視圖類的對象, 驗(yàn)證自定義視圖(布局, 樣式)

?

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(@"加載視圖"); } 視圖已經(jīng)加載完成 - (void)viewDidLoad {NSLog(@"視圖已經(jīng)加載完成");[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]; }當(dāng)應(yīng)用的內(nèi)存快不夠用時(shí), 就會發(fā)出警告, 系統(tǒng)會讓當(dāng)前顯示的視圖控制器執(zhí)行didReceiveMemoryWarning - (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.NSLog(@"收到內(nèi)存警告");UILabel *label = (UILabel *)[self.view viewWithTag:100];從父視圖移除某個(gè)子視圖 [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控制旋轉(zhuǎn)的方法 是否支持旋轉(zhuǎn), 默認(rèn)YES - (BOOL)shouldAutorotate {return YES; }- (NSUInteger)supportedInterfaceOrientations { UIInterfaceOrientationMaskPortrait: 肖像模式UIInterfaceOrientationMaskLandscapeRight: 風(fēng)景模式右UIInterfaceOrientationMaskPortraitUpsideDown: 肖像模式倒置UIInterfaceOrientationMaskLandscape: 風(fēng)景模式(左, 右)UIInterfaceOrientationMaskAll: 四個(gè)方向UIInterfaceOrientationMaskAllButUpsideDown: 除了肖像模式倒置的所有方向iPhone項(xiàng)目 默認(rèn): UIInterfaceOrientationMaskAllButUpsideDowniPad項(xiàng)目 默認(rèn): UIInterfaceOrientationMaskAllreturn UIInterfaceOrientationMaskAll; }視圖將要旋轉(zhuǎn), 執(zhí)行這個(gè)方法 - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {size, 當(dāng)前視圖的大小NSLog(@"%@", NSStringFromCGSize(size));if (size.width > size.height) {NSLog(@"橫屏");view的frame不允許單獨(dú)修改, 必須整體修改//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: 是一種設(shè)計(jì)框架

? ? M: model, 數(shù)據(jù)模型, 用于定義數(shù)據(jù)結(jié)構(gòu), 存儲數(shù)據(jù)

? ? V: view, 視圖, 用于展示內(nèi)容

? ? C: controller, 控制器, 作為modelview的協(xié)調(diào)者

? ? 1.MVC,面向?qū)ο蟪绦蛑谐霈F(xiàn)的對象進(jìn)行分類, 規(guī)定各自的作用

? ? 2.modelview之間不能通信

? ? 3.controller可以訪問modelview, modelview不能訪問controller

? ? 4.view可以間接訪問controller, 主要的通信手段

?? ? a.target-action(目標(biāo)動作機(jī)制)

?? ? b.delegate(代理模式)

?? ? c.dataSource(也是代理模式, 只不過換了一個(gè)名字, 主要用于數(shù)據(jù)的傳遞)

? ? 5.moodel可以間接訪問controller, 主要的通信手段

?? ? a.KVO(鍵值觀察)

?? ? b.notification(通知)

? ? UIViewController, 視圖控制器, 屬于控制類, viewmodel的協(xié)調(diào)者, MVC框架的核心, 繼承于UIResponder, 管理iOS應(yīng)用中的view

? ? appDelegate中的視圖創(chuàng)建的的代碼, 轉(zhuǎn)移到UIViewController

? ? RootViewController *rootVC = [[RootViewController alloc] init];

? ? 指定window的根視圖控制器

? ? self.window.rootViewController = rootVC;

? ? [rootVC release];

? ? : 視圖控制器會自帶一個(gè)UIView, 當(dāng)指定window的根視圖控制器后, 會出現(xiàn)window的上方, 并且和window的大小相同

?

轉(zhuǎn)載于:https://www.cnblogs.com/OrangesChen/p/4895863.html

總結(jié)

以上是生活随笔為你收集整理的自定义视图 视图控制器(UIViewController)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。