AP102 2016/05/27
自訂的類別
//
// DataManager.h
// HelloMyPhotoViewer
//
// Created by Financialbrain on 2016/5/27.
// Copyright © 2016年 DarisCode. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface DataManager : NSObject
//
+(instancetype)sharedInstance;
@end
//
// DataManager.m
// HelloMyPhotoViewer
//
// Created by Financialbrain on 2016/5/27.
// Copyright © 2016年 DarisCode. All rights reserved.
//
#import "DataManager.h"
// _ 代表被包在裡面 沒事不給人家碰
// static 一啟動就配置 不因為任何原因回收記憶體
static DataManager *_singletonDataManager;
@implementation DataManager
//
+(instancetype)sharedInstance{
if(_singletonDataManager == nil){
_singletonDataManager = [DataManager new];
}
return _singletonDataManager;
}
@end
進到MasterViewController.m->#import "DataManager.h"
@interface MasterViewController ()
@property NSMutableArray *objects;
@end
@implementation MasterViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
// Try DataManager
-> DataManager * dataManager = [DataManager sharedInstance];
}
//
// DataManager.h
// HelloMyPhotoViewer
//
// Created by Financialbrain on 2016/5/27.
// Copyright © 2016年 DarisCode. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface DataManager : NSObject
//
+(instancetype)sharedInstance;
-(NSInteger) getTotal;
-(NSString*) getFileNameByIndex:(NSInteger)index;
-(UIImage*) getImageByIndex:(NSInteger)index;
@end
+(instancetype)sharedInstance{
if(_singletonDataManager == nil){
_singletonDataManager = [DataManager new];
[_singletonDataManager loadDatas];
}
return _singletonDataManager;
}
-(void)loadDatas{
datas =[NSMutableArray new];
for (int i =1; i<=10; i++) {
NSString *filename =[NSString stringWithFormat:@"cat%d.jpg",i];
[datas addObject:filename];
}
-(NSInteger) getTotal{
return datas.count;
}
-(NSString*) getFileNameByIndex:(NSInteger)index{
return datas[index];
}
-(UIImage*) getImageByIndex:(NSInteger)index{
NSString *filename = [self getFileNameByIndex:index];
return [UIImage imageNamed:filename];
}
Comments
Post a Comment