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

本次主要学习了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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值