IOS开发------图片浏览器之MVC设计模式

来源:互联网 发布:visio是什么软件 编辑:程序博客网 时间:2024/05/22 19:59

本次主要学习了MVC架构的方式:

①MVC即:Model View Controller    其中Model一般指自己封装的类;View即用户可以操作的界面;Controller即操作界面的后台关联代码。

②新建一个OC的类:继承自NSObject,并且包含头文件 #import <UIKit/UIKit.h> 。为了在类里面可以使用UIImage


代码介绍:

ImageDate.h文件:

#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@interface ImageDate : NSObject//图形@property (strong , nonatomic) UIImage* imagePicture;//图形标题@property (strong , nonatomic) NSString* imageTitle;//使用dictionary来初始化图形-(id) initImageWithDictionary:(NSDictionary*) dict;//为了得到相关的imageDate数据+(NSArray*) readyForPicturePath:(NSString*)path;@end

ImageDate.cpp文件:

#import "ImageDate.h"@implementation ImageDate//创建一个构造方法,构造方法的返回值必须是id- (id)initImageWithDictionary:(NSDictionary *)dict{    //必须调用父类的构造方法    if (self = [super init]) {        //NSDictionary通过键值访问数据        _imageTitle = dict[@"title"];        _imagePicture = [UIImage imageNamed:dict[@"icon"]];    }        return self;}+ (NSArray *)readyForPicturePath: (NSString*)path{    //1.获得bundle包    NSBundle *bundle = [NSBundle mainBundle];    //2.获得plis文件路径    NSString *plistPath = [bundle pathForResource:path ofType:@"plist"];    //3.将文件内容存储到Array    NSArray *array = [NSArray arrayWithContentsOfFile:plistPath ];    NSMutableArray *arrayMutableImage = [NSMutableArray array];    for (NSDictionary *dict in array) {        ImageDate *image = [[ImageDate alloc] initImageWithDictionary:dict];        [arrayMutableImage addObject:image];    }            return arrayMutableImage;}@end

然后是ViewController.h里面的代码:

#import <UIKit/UIKit.h>@interface ViewController : UIViewController//白天夜间模式- (IBAction)LightController:(id)sender;//滑块- (IBAction)mySlider:(id)sender;//加减步长- (IBAction)myStepper:(id)sender;//图片控制器@property (weak, nonatomic) IBOutlet UIImageView *imageController;//图片编号@property (weak, nonatomic) IBOutlet UILabel *imageNumber;//滑块属性@property (weak, nonatomic) IBOutlet UISlider *mySlider;//步长属性@property (weak, nonatomic) IBOutlet UIStepper *myStepper;//图片描述@property (weak, nonatomic) IBOutlet UILabel *imageTitle;@end

ViewController.cpp里面的代码:

#import "ViewController.h"#import "ImageDate.h"@interface ViewController (){    NSInteger _photoIndex;}@property (strong,nonatomic) NSArray *imageArray;@end@implementation ViewController            - (void)viewDidLoad {    [super viewDidLoad];        _imageArray = [ImageDate readyForPicturePath:@"image"];    [self addThePictureAndTitle:0];    _photoIndex = 0;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}#pragma mark 导入图片标题- (void)addThePictureAndTitle:(NSInteger) index{    ImageDate *imageDate = self.imageArray[index];    self.imageController.image = imageDate.imagePicture;    self.imageTitle.text = imageDate.imageTitle;}#pragma mark 夜间模式- (IBAction)LightController:(id)sender {        UISwitch *mySwith = (UISwitch*)sender;        if (mySwith.isOn) {        self.view.backgroundColor = [UIColor greenColor];    } else {        self.view.backgroundColor = [UIColor whiteColor];    }}#pragma mark 控制滑块- (IBAction)mySlider:(id)sender {        UISlider *mySilder = (UISlider*)sender;    NSInteger index = [mySilder value] - 1;    if (index != _photoIndex) {        //获得图形        [self addThePictureAndTitle:index];                self.mySlider.value = index + 1;        _photoIndex = index ;        self.imageNumber.text = [NSString stringWithFormat:@"%.2d/17",index + 1];        NSLog(@"进了if语句");    }    NSLog(@"%f",mySilder.value);    }#pragma mark stepper- (IBAction)myStepper:(id)sender {        UIStepper *myStepper = (UIStepper*)sender;    NSInteger stepperValue = myStepper.value -1;        [self addThePictureAndTitle:stepperValue];        self.mySlider.value = stepperValue + 1;    self.imageNumber.text = [NSString stringWithFormat:@"%.2d/17",stepperValue + 1];    NSLog(@"%.2d",stepperValue);}@end


0 0