UI1_ViewController视图切换及Appdelegate

//
//  ThirdViewController.h
//  UI1_ViewController视图切换及Appdelegate
//
//  Created by zhangxueming on 15/7/3.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ThirdViewController : UIViewController

@end


//
//  ThirdViewController.m
//  UI1_ViewController视图切换及Appdelegate
//
//  Created by zhangxueming on 15/7/3.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "ThirdViewController.h"

@interface ThirdViewController ()

@end

@implementation ThirdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor greenColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(20, 200, self.view.frame.size.width-40, 50);
    btn.backgroundColor = [UIColor whiteColor];
    [btn setTitle:@"切换视图到SecondView" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = 100;
    [self.view addSubview:btn];
}

- (void)btnClicked
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (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



//
//  SecondViewController.h
//  UI1_ViewController视图切换及Appdelegate
//
//  Created by zhangxueming on 15/7/3.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@end



//
//  SecondViewController.m
//  UI1_ViewController视图切换及Appdelegate
//
//  Created by zhangxueming on 15/7/3.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "SecondViewController.h"
#import "AppDelegate.h"
#import "ThirdViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    UIApplication *app = [UIApplication sharedApplication];
    AppDelegate *delegate = app.delegate;
    [delegate.shareArray addObject:@"three"];
    NSLog(@"shareArray = %@", delegate.shareArray);
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeSystem];
    btn1.frame = CGRectMake(20, 200, self.view.frame.size.width-40, 50);
    btn1.backgroundColor = [UIColor whiteColor];
    [btn1 setTitle:@"切换视图到FirstView" forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    btn1.tag = 100;
    [self.view addSubview:btn1];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
    btn2.frame = CGRectMake(20, 300, self.view.frame.size.width-40, 50);
    btn2.tag = 200;
    btn2.backgroundColor = [UIColor whiteColor];
    [btn2 setTitle:@"切换视图到ThirdView" forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn2];
    
}

- (void)btnClicked:(UIButton *)btn
{
    if (btn.tag==100) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else if(btn.tag ==200)
    {
        ThirdViewController *tvc = [[ThirdViewController alloc] init];
        tvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentViewController:tvc animated:YES completion:nil];
    }
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"SecondView 将要显示");
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSLog(@"SecondView 已经显示");
}



- (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
//
//  AppDelegate.h
//  UI1_ViewController视图切换及Appdelegate
//
//  Created by zhangxueming on 15/7/3.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (retain, nonatomic)NSMutableArray *shareArray;

@property (strong, nonatomic) UIWindow *window;

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;


@end



//
//  AppDelegate.m
//  UI1_ViewController视图切换及Appdelegate传值
//
//  Created by zhangxueming on 15/7/3.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.shareArray = [NSMutableArray array];
    }
    return self;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //通过单例方法获取当前应用程序唯一对象
    UIApplication *app=[UIApplication sharedApplication];
    //获取当前应用程序的代理对象
    AppDelegate  *delegate = app.delegate;
    
    [delegate.shareArray addObject:@"one"];
    NSLog(@"shareArray = %@", delegate.shareArray);
    
    return YES;
}
//
//  ViewController.h
//  UI1_ViewController视图切换及Appdelegate
//
//  Created by zhangxueming on 15/7/3.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end



//
//  ViewController.m
//  UI1_ViewController视图切换及Appdelegate
//
//  Created by zhangxueming on 15/7/3.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "ViewController.h"
#import "AppDelegate.h"
#import "SecondViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIApplication *app = [UIApplication sharedApplication];
    AppDelegate *delegate = app.delegate;
    [delegate.shareArray addObject:@"two"];
    NSLog(@"shareArray = %@", delegate.shareArray);
    
    //
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(20, 100, self.view.frame.size.width-40, 50);
    [btn setTitle:@"切换视图到SecondView" forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor whiteColor];
    [btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    self.view.backgroundColor = [UIColor cyanColor];
}


- (void)btnClicked
{
    SecondViewController  *svc = [[SecondViewController alloc] init];
    //设置视图切换模式
    svc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    //
    [self presentViewController:svc animated:YES completion:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    NSLog(@"FirstView 将要消失");
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    NSLog(@"FirstView 已经消失");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
原文地址:https://www.cnblogs.com/0515offer/p/4638590.html