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
时间: 2024-11-25 11:58:27

UI1_ViewController视图切换及Appdelegate的相关文章

iOS:删除storyBoard,纯代码实现UITabBarController的视图切换功能

storyboard是一个很强大的编写代码的辅助工具,可以帮助布局多个视图之间的联系,既直观又能减少代码量:但是,作为一个程序员,在不使用storyboard的情况下,纯代码编写是必须的技能. 下面就用纯代码实现纯代码实现UITabBarController的视图切换功能,咱就实现三个视图之间的转换吧,代码不多,容易看的明白. 步骤: 1.删除storyboard故事板和UIViewController 2.创建三个控制器类,均继承自UIViewController,分别为FirstViewCo

iOS开发系列--视图切换

概述 在iOS开发中视图的切换是很频繁的,独立的视图应用在实际开发过程中并不常见,除非你的应用足够简单.在iOS开发中常用的视图切换有三种,今天我们将一一介绍: UITabBarController UINavigationController 模态窗口 UITabBarController iOS三种视图切换的原理各不相同: UITabBarController:以平行的方式管理视图,各个视图之间往往关系并不大,每个加入到UITabBarController的视图都会进行初始化即使当前不显示在

Tabbar视图切换,返回上一视图,添加item

前面有一篇博文iOS学习之Tab Bar的使用和视图切换 这是在AppDelegate里使用Tabbar,这样的程序打开就是TabbarView了,有时候我们需要给程序做一些帮助页面,或者登录页面,之后才跳转到tabbar View里,或者后面的页面才使用Tabbar的,那这样怎么实现呢? 我们建立一个视图,然后在这个视图通过[selfpresentModalViewController : tabBaranimated:YES];跳转来实现. 当程序中需要在多个View直接切换的时候,可以使用

UI2_视图切换ViewController

// // SubViewController.h // UI2_视图切换 // // Created by zhangxueming on 15/7/3. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <UIKit/UIKit.h> @interface SubViewController : UIViewController @end // // SubViewController.m // UI2

iOS开发——代码生成TabBar与视图切换详解

我在之前多篇博客中讲解了在不使用storyboard而使用nib文件的情况下,使用代码生成导航栏并进行跳转,具体可以参考<iOS开发--界面跳转与返回及视图类型详解><iOS纯代码实现界面建立.跳转.导航栏(无storyboard.无nib)(Objective-C)>.今天我来讲解下在使用nib搭建界面的情况下,用代码生成TabBar,并进行界面之间的跳转.代码示例已经上传至:https://github.com/chenyufeng1991/TabBarTest   . (1)

iOS开发——代码生成TabBar与视图切换具体解释

我在之前多篇博客中解说了在不使用storyboard而使用nib文件的情况下.使用代码生成导航栏并进行跳转,具体能够參考<iOS开发--界面跳转与返回及视图类型具体解释><iOS纯代码实现界面建立.跳转.导航栏(无storyboard.无nib)(Objective-C)>. 今天我来解说下在使用nib搭建界面的情况下,用代码生成TabBar,并进行界面之间的跳转.代码演示样例已经上传至:https://github.com/chenyufeng1991/TabBarTest  

UIView 视图切换

UIView之间常用视图之间切换方式 转载自:http://www.jianshu.com/p/0d53f9402c07 在平时编写代码的过程中,页面之间的跳转可以说就和MVC模式一样是开发必须的.但是可能我们知道一种或者几种页面之间的跳转,今天我就来总结一下我在开发之中遇到的所有的页面跳转代码.(关于控制器之间的简单的跳转,比如导航控制器跳转.故事版跳转.简单的模态跳转不在这里多说) 一.代理跳转. 通常我们在跳转中经常是通过你点击了某个事件或者某个操作使你进行控制器之间的跳转.那么我们可以在

无NavigationBar到有NavigationBar视图切换时的一个坑

NavigationController在iOS App中是最常见不过了,可以说是每个App中必备的了.自iOS7开始,系统自带的右滑返回效果,也可以让有NavigationBar的视图切换很丝 滑流畅.当然你也可能会遇到有些视图并没有NavigationBar(实际上是隐藏了),然后从这样的视图中push一个有NavigationBar 的视图,这种需求很常见(姑且不讨论这种设计到底合不合适),实现起来也很简单,就是在不同的视图里对NavigationBar设置隐藏和显示就可以 了,代码如下:

iOS项目开发实战——实现视图切换动画

不同界面或者说不同视图之间进行切换是应用程序的一种最常见的动态效果,无论是哪一种平台的项目开发,默认的视图切换往往是十分单调的,没有任何动画的,界面的切换也是非常的突兀.如果说使用动画效果使界面能够活跃起来,那么你的App将会非常动感.这里将实现视图切换过程中的动画效果.具体实现如下: (1)本次试验将拖入2张图片,不直接放到View Controller中,而是在代码中动态加载.拖到Main.storyboard中后目录结构如下: . (2)实现图片与代码Outlet绑定: @IBOutlet