UIViewController

来源:互联网 发布:大数据变革时代论文 编辑:程序博客网 时间:2024/05/18 01:59

//

//  RootViewController.m


 

#import "RootViewController.h"

#import "LTView.h"

#import "LView.h"

#import "SecondViewController.h"

 

@interfaceRootViewController ()

 

@end

 

@implementationRootViewController

 

- (instancetype)init{

if (self = [super init]) {

NSLog(@"1");

    }

return self;

}

 

 //  先于init执行

- (instancetype)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];

if (self) {

NSLog(@"初始化");

    }

return self;

 

}

 

 

// 当我们对控制器自带视图不满意时,我们通过重写loadView方法,实现自定义视图。

- (void)loadView{

   [superloadView];

//   UIView *v = [[UIViewalloc]initWithFrame:[UIScreenmainScreen].bounds];

//   v.backgroundColor = [UIColorredColor];

//   // 将我们自定义的视图作为控制器视图

//   self.view = v;

//   [v release];

//   NSLog(@"视图正在加载");

}

 

     // 一般我们的代码都写在ViewDidLoad里面,而不是在loadView中。

- (void)viewDidLoad {

NSLog(@"视图已经被加载");

 [superviewDidLoad];

 

//    // 自定义视图

//   LTView *ltView = [[LTViewalloc]initWithFrame:CGRectMake(50, 30, 300, 30)text:@"账户名" placeHolder:@"请输入"];

//   ltView.backgroundColor = [UIColoryellowColor];

//   [self.viewaddSubview:ltView];

//   [ltView release];

 

LView *lView =[[LViewalloc]initWithFrame:CGRectMake(50, 30, 300, 60) text:@"账户名"placeHolder:@"请输入" buttonText:@"return"];

lView.backgroundColor = [UIColorwhiteColor];

   [self.viewaddSubview:lView];

   [lView.buttonaddTarget:self action:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];

 

   [lView release];

   // 视图控制器功能

   // 1、视图的整体布局,事件响应

   // 2、检测并且处理内存警告

   // 3、检测屏幕旋转

   // 4、视图切换

 

 

}

   //  界面的跳转

- (void)buttonAction:(UIButton *)button{

     // 创建控制器

SecondViewController *secondViewController= [[SecondViewControlleralloc]init];

   // 控制器跳转

    // 第一个参数:将要跳转哪个控制器

   // 第二个参数: 有无动画效果

   // 第三个参数:完成之后什么事情

 

   // 设置界面跳转的动画效果

secondViewController.modalTransitionStyle =UIModalTransitionStyleCoverVertical;  // 特殊不加按钮都可以返回

   [selfpresentViewController:secondViewControlleranimated:YEScompletion:^{

NSLog(@"又被跳转了");

   }];

 

   [secondViewController release];

}

 

   // 检测并且处理内存问题

- (void)didReceiveMemoryWarning {

   [superdidReceiveMemoryWarning];

NSLog(@"坑爹的内存");

if ([self isViewLoaded]&&self.view.window == nil) {

self.view = nil;     // 等于 [self.viewrelease];

    }

}

 

 //  检测屏幕旋转

 //iOS 5.0 之后默认可以旋转

- (BOOL)shouldAutorotate{

 

return YES;

}

 

   //屏幕即将被旋转

-(void)viewWillTransitionToSize:(CGSize)sizewithTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{

NSLog(@"屏幕即将旋转");

}

 

// 屏幕旋转方向

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{

returnUIInterfaceOrientationMaskAll;

}

 

@end

 

 

 

 

 

 

 

//

// SecondViewController.m


 

#import "SecondViewController.h"

 

@interfaceSecondViewController ()

 

 

 

@implementationSecondViewController

 

- (void)viewDidLoad {

   [superviewDidLoad];

self.view.backgroundColor =[UIColoryellowColor];

UIButton *button =[UIButtonbuttonWithType:UIButtonTypeCustom];

button.frame = CGRectMake(100, 100, 60,30);

   [buttonsetTitle:@"return" forState:UIControlStateNormal];

   [buttonsetTitleColor:[UIColorblueColor] forState:UIControlStateNormal];

   [buttonaddTarget:self action:@selector(Return) forControlEvents:UIControlEventTouchUpInside];

   [self.viewaddSubview:button];

}

 

   // 返回上一个界面

- (void)Return{

   [selfdismissViewControllerAnimated:YES completion:^{

 

   }];

}

- (void)didReceiveMemoryWarning {

   [superdidReceiveMemoryWarning];

   // Dispose of any resources that can be recreated.

}

 

 

@end

 

 

 

 

//  LTView.m

 

#import "LTView.h"

 

@implementationLTView

 

// 自定义视图类LTView

//使用了⼀种设计模式:复合设计模式.复合设计模式:A类中,使⽤用B类(或者更多类)作为⾃⼰的成员(实例变量).iOS中复合是特别常见的设计模式。iOS新控件往都是用已有控件组合而成的。

//⾃定义视图:系统标准UI之外,⾃己组合⽽出的新的视图。iOS提供了很多UI组件,借助它们,我们可以做各种程序。尽管如此,实际开发中,我们还需⾃自定义视图。积累⾃自⼰己的代码库。方便开发。⾃自⼰己封装的视图,能像系统UI控件⼀一样,⽤用于别的项⺫中,能大降低开发成本,提⾼开发效率。

 // 以下是初始化一个自定义控件

- (instancetype)initWithFrame:(CGRect)frametext:(NSString *)text placeHolder:(NSString *)placeHolder{

CGFloat w = frame.size.width;

CGFloat h = frame.size.height;

if (self = [super initWithFrame:frame]) {

self.label =[[UILabelalloc]initWithFrame:CGRectMake(0, 0, w / 3, h)];

self.label.backgroundColor =[UIColorredColor];

self.label.text = text;

       [selfaddSubview:self.label];

 

self.TF =[[UITextFieldalloc]initWithFrame:CGRectMake(w / 3 + 10, 0, 2 * w / 3 - 10,h)];

self.TF.backgroundColor = [UIColorbrownColor];

self.TF.borderStyle =UITextBorderStyleRoundedRect;

 

self.TF.layer.borderColor =[UIColorblackColor].CGColor;

self.TF.layer.borderWidth = 1;

self.TF.layer.cornerRadius = 10;

self.TF.placeholder = placeHolder;

       [selfaddSubview:self.TF];

 

       [self.TF release];

       [self.label release];

    }

return self;

}

 

 

- (void)dealloc{

   [_label release];

   [_TF release];

   [superdealloc];

}

@end

 

 

0 0