iOS UI 02 UIimageview 和模态切换

注意这是哪一个文件!!!RootViewController.m 

//  RootViewController.m

//  Ui03  - UIimageView

//

//  Created by dllo on 15/11/10.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import "RootViewController.h"

#import "RootView.h"

#import "SecondViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController


//ViewController的初始化方法

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

//{

//    self = [super initWithNibName: bundle:];

//    if (self) {

//

//    }

//    return self;

//}


- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    NSLog(@"nihao");

}

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    RootView *rootV = [[RootView alloc]initWithFrame:[[UIScreen mainScreen] bounds] ];

    [self.view addSubview:rootV];

    [rootV release];

    

    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(150, 150, 150, 150);

    button.backgroundColor = [UIColor redColor];

    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchDragInside];

    [self.view addSubview:button];

    

    NSLog(@"a");

    

    //UIImage - 专门用来管理图片的类,本身不具备显示功能

    //UIImageView- 专门用来显示图片的视图

    //添加图片的2中方法

    //1 

//    UIImage *image = [UIImage imageNamed:@"image4.jpg"];

//    UIImageView *imagev = [[ UIImageView alloc]initWithImage:image];

//    imagev.frame =  self.view.frame;

//    [self.view addSubview:imagev];

//    [imagev release];

    //2 常用

//    UIImageView *imagev = [[UIImageView alloc]initWithFrame:CGRectMake(30, 30, 300, 500)];

//    imagev.image = [UIImage imageNamed:@"image9.jpg"];

//    [self.view addSubview:imagev];

//    [imagev release];

    

    

    

    

    

    

    

    

    

    

    

    // Do any additional setup after loading the view.

}

- (void)buttonAction

{

    //三下回车  秘诀  选中最后一组,3次回车

    SecondViewController *secondVC = [[SecondViewController alloc]init];

    //切换页面

    //参数1, 要切换的界面

    //参数2, 是否需要动画特效

    //参数3 , block -

    

    

    [self presentViewController:secondVC animated:YES completion:^{

        

        //切换完成调用

       // NSLog(@"%d", __LINE__);

    }];

    

    //presentViewController会引起引用计数增加

   // NSLog(@"%ld", secondVC.retainCount);

    //先执行完成此方法(buttonAction),再去切换界面

   // NSLog(@"%d", __LINE__);

    [secondVC release];

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    

    NSLog(@"接受内存告警");

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end

//

//  SecondViewController.m

//  Ui03  - UIimageView

//

//  Created by dllo on 15/11/10.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import "SecondViewController.h"


@interface SecondViewController ()


@end


@implementation SecondViewController



- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor greenColor];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(150, 150, 150, 150);

    button.backgroundColor = [UIColor redColor];

    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchDragInside];

    [self.view addSubview:button];

    NSLog(@"bbb");

    

    // Do any additional setup after loading the view.

}

- (void)buttonAction

{

    //切回原来界面

    //参数1 - 是否需要动画

    //参数2 - block- 切换完成调用

    //注意新建的rootVc的方式是切换到新的rootVC界面,而非切回原来的rootVC界面

    [self dismissViewControllerAnimated:YES completion:^{

        

        //NSLog(@"%d", __LINE__);

    }];

   // NSLog(@"%d", __LINE__);

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation

 

 


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end


//

//  RootView.m

//  Ui03  - UIimageView

//

//  Created by dllo on 15/11/10.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import "RootView.h"


@implementation RootView

- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        [self createSubviews];

    }

    return self;

}

- (void)createSubviews

{

    //UIImage - 专门用来管理图片的类,本身不具备显示功能

    //UIImageView- 专门用来显示图片的视图

    //添加图片的2中方法

    //1

    //    UIImage *image = [UIImage imageNamed:@"image4.jpg"];

    //    UIImageView *imagev = [[ UIImageView alloc]initWithImage:image];

    //    imagev.frame =  self.view.frame;

    //    [self.view addSubview:imagev];

    //    [imagev release];

    //2 常用

    self.backgroundColor = [UIColor whiteColor];

    UIImageView *imagev = [[UIImageView alloc]initWithFrame:CGRectMake(30, 30, 300,500)];

    imagev.image = [UIImage imageNamed:@"image9.jpg"];

    [self addSubview:imagev];

    [imagev release];


    

}

@end




原文地址:https://www.cnblogs.com/yuhaojishuboke/p/5043089.html