UI基础:视图控制器.屏幕旋转.MVC

来源:互联网 发布:外交 知乎 编辑:程序博客网 时间:2024/04/30 06:26

UIViewController 视图控制器,继承自UIResponder,作用:管理视图并且响应事件
功能:
1.分担APPdelegate的工作
2.实现模块独立,能提高复用性
创建UIViewController对象:

UIViewController *viewController=[[UIViewController alloc]init];

UIViewController 自身带了一个UiView,默认的大小和屏幕大小一样.
每一个window都带有一个根视图,如果不给根视图赋值,默认根视图就是它本身(window)

self.window.rootViewController=viewController;//设置self.window的根视图

自定义的视图控制器

@implementation MyViewController//指定初始化方法(当要初始化对象时,一定会走的方法)//一般不在UIViewController指定初始化方法里做任何操作-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        NSLog(@"%s",__FUNCTION__);    }    return self;}//加载视图-(void)loadView {    //如果写了这个方法一定要对UIViewController自带的view初始化    self.view=[[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds] ];    //    重写父类的方法    [super loadView];    //    如果以上操作都不做,loadView和viewDidLoad会走两遍,这时候就出现莫名的错误    NSLog(@"%s",__FUNCTION__);}//视图加载完成-(void)viewDidLoad{    //    写了这个必须要对UIViewController自带的 view 初始化    [super viewDidLoad];//    设置视图控制器view的背景颜色    self.view.backgroundColor=[UIColor grayColor];    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(50, 20, 200, 40)];    label.backgroundColor=[UIColor yellowColor];    label.text=@"这是标签";    [self.view addSubview:label];    [label release];    UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(50, 60, 200, 40)];    textField.backgroundColor=[UIColor redColor];   textField.placeholder=@"文本输入框";//    为textfield设置代理    textField.delegate=self;    [self.view addSubview:textField];    [textField release];    UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];    button.frame=CGRectMake(50, 100, 200, 40);    button.backgroundColor=[UIColor cyanColor];    [button setTitle:@"Button" forState:UIControlStateNormal];    //    给button添加点击事件    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:button];    [button release];    NSLog(@"%s",__FUNCTION__);}//接收内存警告- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    NSLog(@"清理内存吧");    // Dispose of any resources that can be recreated.}#pragma mark --button点击事件---(void)buttonAction:(UIButton *)sender{    self.view.backgroundColor=[UIColor colorWithRed:COLORVALUE green:COLORVALUE blue:COLORVALUE alpha:1.0];}#pragma mark --实现代理方法---(BOOL)textFieldShouldReturn:(UITextField *)textField{    [textField resignFirstResponder];    return YES;}@end

屏幕旋转

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    self.view.backgroundColor=[UIColor greenColor];    UIView *topview=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 200, 100)];    topview.backgroundColor=[UIColor cyanColor];    [self.view addSubview:topview];    topview.tag=100;    [topview release];}//支持旋转的方法//是否允许旋转-(BOOL)shouldAutorotate{    return YES;}//允许的旋转方向-(NSUInteger)supportedInterfaceOrientations{    return UIInterfaceOrientationMaskAll;}//屏幕旋转的时候走的方法.iOS8.0会才支持-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{//    拿到屏幕的宽和高    NSStringFromCGSize(size);    if (size.width>size.height) {        //横屏        UIView *view=[self.view viewWithTag:100];        view.frame=CGRectMake(0, 50,size.width*2/3, 100);    }else{        UIView *view=[self.view viewWithTag:100];        view.frame=CGRectMake(0, 50,size.width*2/3, 100);    }    NSLog(@"屏幕旋转了");}

UIViewController是MVC设计模式的核⼼。
MVC是⼀个框架级的设计模式。
M是Model,主要⽤于建⽴数据模型(即数据的结构)
V是View,我们能看到的所有控件都是view,view主要的功能是展⽰数据。
C是控制器,主要是控制M和V的通信。

0 0
原创粉丝点击