UI_12 ModalViewController(模态),单例设计模式

?、模态viewController

1、介绍

程序中切换??,可以使?UINavigationController。通过导航功能实现??切换。使用    pushViewController:animated:该方法显示的视图具有层级关系;而使用模态视图控制器presentViewController:animated:completion显示的视图与之前的视图控制器平级(或者说是两个不相干的层级)。

使用场景

临时展??些内容。例如:程序中?户登录,通讯录中添加联系?等等。

UIImagePickerController。调?系统相册、照相机。

2、方法

显示:

  • presentViewController:animated:completion:

弹出:

  • dismissViewControllerAnimated:completion:

弹出的两种实现?式,在模态controller中使?:

  • [self dismissViewControllerAnimated:YES completion:nil];
  • [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

使?self调??法,系统会使?self.presentingViewController调?这个?

法。

3、属性

  • modalPresentationStyle 模态controller显?样式
  • modalTransitionStyle 模态显?动画样式


?、单例

在一个应用程序中,可能会有一些信息在程序运行期间一直存在,比如:登录信息、通讯录中的联系人信息等,单例为此类信息的访问提供了便捷。

1、单例的特性

  • 在内存中只有一个对象,节省空间
  • 单例对象存储在堆区,操作单例对象的变量存储在静态区,程序关闭后由系统?动回收
  • 可全局访问
  • 避免频繁的创建销毁对象,可以提?性能
  • 生命周期与应用程序相同
  • 降低模块之间的耦合度,降低代码的复杂度。

2、单例初始化方法

规范写法:

  • standardxxx
  • onlyxxx
  • defaultxxx
  • sharedxxx


三、综合使用

新建RootViewController:UIViewController作为NavigationController的rootViewController。再将NavigationController添加为window的rootViewController。设置RootViewController的leftBar和rightBar分别以模态视图控制器和普通视图控制器的方式推出ModalViewController和FirstViewController。

在ModalViewController中初始化单例DataHandle,在FirstViewController中显示单例内容。

代码如下:

AppDelegate.m

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

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

RootViewController *rootVC = [[RootViewController alloc] init];

UINavigationController *NC = [[UINavigationController alloc] initWithRootViewController:rootVC];

self.window.rootViewController = NC;

self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;

}

单例

DataHandle.h

@interface DataHandle : NSObject

@property(nonatomic, retain) NSString *text;

+ (DataHandle *)shareInstance;

@end

DataHandle.m

@implementation DataHandle
static DataHandle *handle = nil;
+ (DataHandle *)shareInstance
{
    //同步锁(为了避免多个线程同时访问同一个资源,导致资源不统一)
    @synchronized (self){
        if (!handle) {
            handle = [[DataHandle alloc] init];
        }

}

return handle;

}

@end

模态

ModalViewController.m

@interface ModalViewController ()
@property(nonatomic, retain) UITextField *textField;
@end

@implementation ModalViewController

- (void)dealloc
{
    [_textField release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor magentaColor];
   
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 30)];
    _textField.borderStyle = UITextBorderStyleRoundedRect;
   
    [self.view addSubview:_textField];

[_textField release];

[self createBarButtonItems];

}

- (void)createBarButtonItems
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Dismiss" style:UIBarButtonItemStylePlain target:self action:@selector(didClickLeftBI:)];
}

- (void)didClickLeftBI:(UIBarButtonItem *)BI
{
    //模态返回
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"模态返回");
    }];
    //获取单例对象
    DataHandle *handle = [DataHandle shareInstance];
   
    //为单例对象赋值
    handle.text = _textField.text;
   
}

@end

RootViewController.m

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Modal" style:UIBarButtonItemStylePlain target:self action:@selector(toModalViewController:)];
   
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"PUSH" style:UIBarButtonItemStylePlain target:self action:@selector(toNextView:)];
   
    self.title = @"RootVC";
   
    self.view.backgroundColor = [UIColor blueColor];
   
    // Do any additional setup after loading the view.
}

- (void)toModalViewController:(UIBarButtonItem *)BI
{
   
    ModalViewController *modalVC = [[ModalViewController alloc] init];
    //模态推出modalVC,推出的modalVC和navigationController平级
    //设置modalTransition
    modalVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
   
    //给modalVC添加navigationController
    UINavigationController *NC = [[UINavigationController alloc] initWithRootViewController:modalVC];
   
    [self presentViewController:NC animated:YES completion:^{

NSLog(@"模态推出");

}];

[modalVC release];
    NSLog(@"feqr");
}

- (void)toNextView:(UIBarButtonItem *)BI
{

FirstViewController *firstVC = [[FirstViewController alloc] init];

[self.navigationController pushViewController:firstVC animated:YES];

}

@end

FirstViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
   
    //获取单例对象
    DataHandle *dataHandle = [DataHandle shareInstance];
    self.title = dataHandle.text;
   
    self.view.backgroundColor = [UIColor redColor];
    // Do any additional setup after loading the view.

}

时间: 2024-08-27 07:31:20

UI_12 ModalViewController(模态),单例设计模式的相关文章

单例设计模式

一:单例设计模式的定义 单例设计模式,顾名思义,就是在整个程序运行过程中,只向外界提供一个对象,这样做可以避免资源的浪费,例如 我们打开回收站或者ppt时,只会启动一个窗口. 单例模式的java实现: 1:饿汉式 1 /** 2 * 3 */ 4 package com.hlcui.singleton; 5 6 /** 7 * @author Administrator 饿汉式单例类 8 */ 9 public class SingletonDemo { 10 // 1:内部创建一个对象 11

iOS 中的单例设计模式

单例设计模式:在它的核心结构中只包含一个被称为单例类的特殊类.例如文件管理中的NSUserDefault,应用程序中的UIApplication,整个应用程序就这一个单例类,负责应用程序的一些操作,单例在那个文件下都能取得到. 通过单例设计模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节省系统资源.如果希望在系统中某个类的对象只能存在一个,单例模式是最好的选择. 下面来点实在的,创建单例代码上 方法1:基于线程安全创建一个单例 .h做一下声明 + (id)

设计模式之单例设计模式

一.何为单例设计模式 单例模式,顾名思义就是单个实例,程序中某个类只有一个实例存在.通常实在需要共享某个资源避免资源损耗的情况下使用到的. 二.单例设计模式的代码实现 一说到单例模式的概念,我们首先会想到下面的这种的写法 public class SingleInstance { private static SingleInstance singleInstance; /** * 单例模式 * @return */ public static SingleInstance getSingleI

设计模式整理_单例设计模式

单例设计模式,它确保一个类只有一个实例,并提供一个全局访问点. 由于单例设计模式对应的类只能创建一个对象,因此它所对应的方法必须是static(不是static只有创建对象才能调用). 以下是单例模式的一个经典实现:采用了延迟加载对象的例子. public class Single1 { private static Single1 single; //利用一个静态变量来记录Single1的唯一实例,这里没有直接声明,采用了延迟加载模式. private Single1(){} //把构造器声明

【学习笔记】单例设计模式笔记

单例设计模式是常见的设计模式之一.通过单例实现所需求类在系统中只存在唯一一个实例. 单例设计模式分两种:懒汉单例设计模式和饿汉单例设计模式,两者设计思路一致,实现有微小不同. 实现代码: 1 public class HungryMan { 2 3 private HungryMan(){};//私有的构造方法保证HungryMan类无法在外部使用构造方法实例化 4 private static final HungryMan hungryMan=new HungryMan();//在类内定义一

IOS开发之单例设计模式

本文将从四个方面对IOS开发中的单例设计模式进行讲解: 一.什么是单例设计模式 二.我们为什么要用单例设计模式 三.单例设计模式的基本用法 四.自定义单例设计模式代码的封装 一.什么是单例设计模式 所谓单例,即是单个的实例化对象,保证一个类有且仅有一个实例.通常情况下,当我们对一个类实例化时(如:alloc.new等),并不能保证每次实例化的对象是唯一的实例.那么为了保证该类可在多次实例化的过程中保证内存地址不变,就需要引入单例设计模式. 二.我们为什么要用单例设计模式 1.Singleton

Java——单例设计模式

设计模式:解决某一类问题最行之有效的方法.Java中23种设计模式:单例设计模式:解决一个类在内存中只存在一个对象. 想要保证对象唯一.1,为了避免其他程序过多建立该类对象.先禁止其他程序建立该类对象2,还为了让其他程序可以访问到该类对象,只好在本类中,自定义一个对象.3,为了方便其他程序对自定义对象的访问,可以对外提供一些访问方式 这三部怎么用代码体现呢?1,将构造函数私有化.2,在类中创建一个本类对象.3,提供一个方法可以获取到该类对象. 对于事物该怎么描述,还怎么描述.当需要将该事物的对象

OC中的单例设计模式及单例的宏抽取

1 // 在一个对象需要重复使用,并且很频繁时,可以对对象使用单例设计模式 2 // 单例的设计其实就是多alloc内部的allocWithZone下手,重写该方法 3 4 #pragma Person.h文件 5 6 #import <Foundation/Foundation.h> 7 @interface Person : NSObject <NSCopying,NSMutableCopying> 8 + (instancetype)sharePerson; // 给类提供一

微信 使用单例设计模式 提供AccessToken 和Jsapi_ticket缓存支持

上一篇 是使用ecache 做的缓存, 有些简单的微信项目并不需要这么复杂,So就想到单例设计模式  首先,我们先定义一个单例对象 import java.util.HashMap; import java.util.Map; /**  *   * @author wangiegie  * @date 2015年9月29日下午8:13:06  * @description  */ public class Singleton { //缓存accessToken 的Map  ,map中包含 一个a

java的单例设计模式

/* 单例设计模式解决的问题:可以保证一个类在内存中对象唯性一性(数据实现了共享). 如何保证对象唯一性呢?1,不允许其他程序用 ,2,在该类创建一个本实例.3,对外提供一个方法让其他程序可以获取该象.步骤:1,私有化该类构造函数. 2.通过 new 在本类中创建一个对象. 3,定义一个公有的方法,将创建对象返回.*/ public class Test1 { public static void main(String[] args) { // TODO 自动生成的方法存根 Test5 t1=