模态、单例、单例模式

模态视图与单例模式详解

1.模态视图控制器:是一种方式显示的控制器

模态视图控制器常用的几种切换页面的方法

//方法一:显示下个页面控制器

 [self presentViewController:addContactNC animated:YES completion:nil]

//方法二:显示下个页面控制器

self showDetailViewController:<#(UIViewController *)#> sender:<#(id)#>

//取消当前页面控制器
  [self dismissViewControllerAnimated:YES completion:nil];

注意:模态视图不在栈内,是临时页面.

模态视图的两个常用属性:

modalPresentationStyle  模态controller显示样式

modalTransitionStyle       模态显示动画样式


2.单例:唯一性,如数据源,判断是否存在,若存在,则创建,若不存在,则创建,以后就是用该对象,实现资源共享

一般将创建单例对象的方法设置为类方法

如创建一个数据管理类:

.h中声明的类方法

#pragma mark---------声明创建单例的类方法
+(instancetype)sharedDataManager;


#pragma mark----------声明方法


//添加
-(void)addContact:(Contact *)contact;


//删除
-(void)deleteContactWithIndexPath:(NSIndexPath *)indexPath;


//修改
-(void)changeContactIndexPath:(NSIndexPath *)indexPath withNewContact:(Contact *)contact;



//查询全部联系人
-(NSArray *)findAllContact;


//根据条件查找联系人
-(Contact *)findContactWithIndexPath:(NSIndexPath *)indexPath;



//移动联系人的位置
-(void)moveFromSourceIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)toIndexPath;



.m中实现


@interface DataManager ()


#pragma mark--------声明私有属性
@property(nonatomic,strong) NSMutableArray *allDataArray;


@end




@implementation DataManager


+(instancetype)sharedDataManager
{
    //声明静态变量,设置为空
    static DataManager *dataManager = nil;
    
    //判断是否存在,如果不存在,则创建
    if (dataManager == nil) {
        dataManager = [[DataManager alloc] init];
    }
    
    //如果存在,则返回该对象
    return dataManager;
    
}




#pragma mark---------定义懒加载方法
-(NSMutableArray *)allDataArray
{
    
    if (_allDataArray == nil) {
        
        self.allDataArray = [NSMutableArray array];
    }
    
    return _allDataArray;
}


//添加
-(void)addContact:(Contact *)contact
{
    
    [self.allDataArray addObject:contact];
    
}


//删除
-(void)deleteContactWithIndexPath:(NSIndexPath *)indexPath
{
    [self.allDataArray removeObject:self.allDataArray[indexPath.row]];
}


//修改
-(void)changeContactIndexPath:(NSIndexPath *)indexPath withNewContact:(Contact *)contact
{
    self.allDataArray[indexPath.row] = contact;
}




//查询全部联系人
-(NSArray *)findAllContact
{
    //copy:拷贝一份副本,防止外界操作该数组,此处是深拷贝,返回的是不可变的数组
    //如果用mutableCopy:则返回的是可变的数组
    return [self.allDataArray copy];
}


//根据条件查找联系人
-(Contact *)findContactWithIndexPath:(NSIndexPath *)indexPath
{
    return self.allDataArray[indexPath.row];
}

//移动联系人的位置
-(void)moveFromSourceIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{

    //找到要移动的联系人
    Contact *c = self.allDataArray[sourceIndexPath.row];
    
    //NSLog(@"%@",c);
   
   //从联系人数组中删除该联系人
   [[DataManager sharedDataManager] deleteContactWithIndexPath:sourceIndexPath];

    [self.allDataArray insertObject:c atIndex:toIndexPath.row];

}

注意:

1、操作单例对象的变量存储在静态区,程序关闭后由系统回收。

2、单例对象存储在堆区,不释放,程序关闭后由系统回收。

3、变量和单例对象的生命周期与程序同步。










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值