UIViewController

一,UIViewController的使用

#import "AppDelegate.h"

#import "MainViewController.h"


@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColorwhiteColor];

    [self.windowmakeKeyAndVisible];

    [_windowrelease];

    

    // UIViewController的使用

    // UIViewController是一个抽象类,抽象类不可以直接使用,只能通过创建它的子类去使用。

    MainViewController *mainVC = [[MainViewControlleralloc] init];

    

    // mainVC指定为window的根视图控制器,作为首页显示

    self.window.rootViewController = mainVC;

    

    // 内存管理

    [mainVC release];

    

    returnYES;

}



- (void)dealloc

{

    [_windowrelease];

    [superdealloc];

}

二,创建UIViewController

#import "MainViewController.h"

#import "SecondViewController.h"


@interface MainViewController ()


@end


@implementation MainViewController


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

{

    self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        NSLog(@"初始化视图控制器");

    }

    returnself;

}


- (void)loadView

{

    [superloadView];

    NSLog(@"加载视图");

}



- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    

    NSLog(@"加载视图完毕");

    // 一个视图控制器的生命周期

    

    // 1. 初始化方法

    // 2. view显示之前,先调用loadView方法加载view

    // 3. 调用viewDidLoad方法,对视图结构进行布局

    

    // 4. viewWillAppear: 方法

    // 5. viewDidAppear: 方法

    

    

    // 每个视图控制器都自己带一个view,方便开发者添加视图

    

//    self.view.backgroundColor = [UIColor purpleColor];

    

    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];

    button.frame =CGRectMake(20,200, 80,50);

    button.backgroundColor = [UIColororangeColor];

    [button setTitle:@"点击"forState:UIControlStateNormal];

    [button addTarget:selfaction:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:button];

}


- (void)buttonClicked:(UIButton *)button

{

    NSLog(@"GOOD");

    

    // 弹出第二个页面

    

    // 1. 创建第二个页对象

    SecondViewController *secondVC = [[SecondViewControlleralloc] init];

    

    [secondVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];

    

    // 2. 弹出

    [selfpresentViewController:secondVC animated:YEScompletion:^{

        NSLog(@"完成弹出");

    }];

    

    // 3. 内存管理

    [secondVC release];

    

    

    

}


- (void)viewWillAppear:(BOOL)animated

{

    [superviewWillAppear:animated];

    NSLog(@"视图将要出现");

}


- (void)viewDidAppear:(BOOL)animated

{

    [superviewDidAppear:animated];

    NSLog(@"视图已经出现");

}


- (void)viewWillDisappear:(BOOL)animated

{

    [superviewWillDisappear:animated];

    NSLog(@"视图将要消失");

}


- (void)viewDidDisappear:(BOOL)animated

{

    [superviewDidDisappear:animated];

    NSLog(@"视图已经消失");

}

三,第二个UIViewController页面

#import "SecondViewController.h"

#import "MainViewController.h"


@interface SecondViewController ()


@end


@implementation SecondViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    self.view.backgroundColor = [UIColor grayColor];

    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    button.frame = CGRectMake(50, 300, 100, 80);

    button.backgroundColor = [UIColor whiteColor];

    [button setTitle:@"点击" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    

}


- (void)buttonClicked:(UIButton *)button

{

    NSLog(@"great");

    // 干掉自己

    [self dismissViewControllerAnimated:YES completion:^{

        NSLog(@"返回结束");

    }];

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值