1.創建了一個UIview類
2.重寫新類的 初始化方法
3.把想添加的視圖 封裝到新類裏面(初始化到新類中)
4.爲了方便外部進行賦值取值把添加的視圖寫成屬性(別忘了deacllo釋放)
5.測試一下;
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
self.label = [[UILabel alloc]initWithFrame: CGRectMake(0, 0,self.frame.size.width/3, self.frame.size.height)];
self.label.backgroundColor = [UIColor clearColor];
self.label.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.label];
[_label release];
self.text = [[UITextField alloc]initWithFrame:CGRectMake(self.frame.size.width/3+20, 0, self.frame.size.width/3*2-20, self.frame.size.height)];
self.text.borderStyle = UITextBorderStyleRoundedRect;
[self addSubview:self.text];
[_text release];
//獲取動態寬度與高度
// CGFloat width = frame.size.width;
// CGFloat height = frame.size.height;
}
return self;
MVC 是一種設計模式
好處:讓視圖(view)可以複用
應用:
1.視圖層(view) 只寫視圖 的佈局
2.數據模型層(model)只寫數據的結構(只寫ns開頭的)
3.控制器(control)負責邏輯部分(例如從model中取數據然後區更改視圖顯示)
MVC 把工程中的代碼模塊化 儘量讓視圖部分代碼複用降低耦合度
一般一個視圖控制器控制一個頁面
[self.window makeKeyAndVisible];
// 顯示控制器自帶的view
// 給window設置一個根視圖控制器
// 初始化控制器
RootViewController *rootVC = [[RootViewController alloc]init];
// 設置根視圖控制器
[self.window setRootViewController:rootVC];
// 釋放
{
// 調用父類的方法去加載自身視圖0
// [super loadView];
// 幫系統給賦值
self.view = login;
login.tag = 1000;
login.username.tag = 1100;
login.password.tag = 1101;
[login release];
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//更改自身view的顏色
self.view.backgroundColor = [UIColor tiankonglan];
//控制器中寫邏輯部分
// 給button添加一個點擊方法
LoginView *view =(LoginView *)self.view;
[view.login addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
view.username.text.delegate = self;
view.password.text.delegate = self;
屏幕旋轉
屏幕橫屏改變原來佈局
豎屏時 豎屏佈局
1.允許屏幕選擇
2.指定屏幕旋轉的方向
3.找到旋轉觸發方法
4.判斷屏幕方向更改佈局
5.測試一下
*/
//允許屏幕旋轉
-(BOOL)shouldAutorotate
{
return YES;
}
// 指定屏幕旋轉方向
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
// 旋轉時觸發的方法
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
NSLog(@"%@",NSStringFromCGSize(size));
//重新佈局子視圖
//frame發生變化時候會觸發layoutSubview方法
-(void)layoutSubviews
{
//不知道父類做了什麼所有需要在從寫的時候先調用父類方法
//然後再寫自己的方法
[super layoutSubviews];
//frame 發生變化相當於 橫屏了
//更改佈局
//判斷豎屏還是橫屏
//1.把應用程序取出來
//2.判斷一下當前應用程序屏幕朝向
//單例方法的命名規範share什麼
UIApplication *app = [UIApplication sharedApplication];
//判斷方向
if (app.statusBarOrientation == UIInterfaceOrientationPortrait||app.statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown) {
self.password.frame = CGRectMake(self.username.frame.origin.x, self.username.frame.origin.y+KRow, self.username.frame.size.width, self.username.frame.size.height);
}else
{
self.password.frame = CGRectMake((self.username.frame.origin.x+10 + self.username.frame.size.width), self.username.frame.origin.y, self.username.frame.size.width, self.username.frame.size.height);
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
NSLog(@"內存警告了");
// 釋放已經顯示過的並且不是當前顯示的視圖
// 如果這個視圖正在被顯示那麼這個視圖的window屬性不是空的
if ([self isViewLoaded] == YES && self.view.window ==nil) {
//把當前視圖 釋放
self.view = nil;
}
//添加子控制器
self.secondVC = [[SeconViewController alloc]init];
//把secondVC添加成子控制器 讓rootVC去管理
[self addChildViewController:self.secondVC];
//把seondVC.view 添加到 rootVC.view上
[self.view addSubview:self.secondVC.view];
//釋放
[self.secondVC release];
self.view.backgroundColor = [UIColor redColor];
//添加一個按鈕移除移除子視圖控制器
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 100);
button.backgroundColor = [UIColor brownColor];
//添加方法刪除視圖
[button addTarget:self action:@selector(delete:) forControlEvents:UIControlEventTouchUpInside];
//添加到rootVC.view上
[self.view addSubview:button];
{
// 視圖刪了
[self.secondVC.view removeFromSuperview];
// 移除子控制器
[self.secondVC removeFromParentViewController];
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"視圖就要出現");
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(@"視圖已經出現");
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
NSLog(@"視圖將要消失");
}
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
NSLog(@"視圖已經消失");