故事板控制器:
//
// XMGViewController.m
// 03-综合练习
//
// Created by xiaomage on 15/12/28.
// Copyright © 2015年 小码哥. All rights reserved.
//
#import "XMGViewController.h"
#import "XMGShop.h"
#import "XMGShopView.h"
@interface XMGViewController ()
// 购物车
@property (weak, nonatomic) IBOutlet UIView *shopCarView;
// 添加按钮
@property (weak, nonatomic) IBOutlet UIButton *addButton;
// 删除按钮
@property (weak, nonatomic) IBOutlet UIButton *removeButton;
/** 数据数组 */
@property (nonatomic, strong) NSArray *dataArr;
@end
@implementation XMGViewController
/**
* 懒加载
*/
// 初始化数据
/**
* 添加到购物车
*
* @param button 按钮
*/
/***********************2.创建一个商品*****************************/
XMGShopView \*shopView = \[XMGShopView shopView\];
shopView.frame = CGRectMake(x, y, width, height);
// 设置数据
shopView.shop = self.dataArr\[index\];//set方法
\[self.shopCarView addSubview:shopView\];
/***********************4.设置按钮的状态*****************************/
button.enabled = (index != );
// 5.设置删除按钮的状态
self.removeButton.enabled = YES;
}
/**
* 从购物车中删除
*
* @param button 按钮
*/
(IBAction)remove:(UIButton *)button {
// 1. 删除最后一个商品
UIView *lastShopView = [self.shopCarView.subviews lastObject];
[lastShopView removeFromSuperview];
// 3. 设置添加按钮的状态
self.addButton.enabled = YES;
// 4. 设置删除按钮的状态
self.removeButton.enabled = (self.shopCarView.subviews.count != );
}
@end
xib界面:
xib界面对应的类:
//
// XMGShopView.h
#import
@class XMGShop;
@interface XMGShopView : UIView
/** 数据模型 */
@property (nonatomic, strong) XMGShop *shop;
// 快速构造方法
//
// XMGShopView.m
#import "XMGShopView.h"
#import "XMGShop.h"
@interface XMGShopView ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;//跟xib界面连线了的
@property (weak, nonatomic) IBOutlet UILabel *titlelabel;//跟xib界面连线了的
@implementation XMGShopView
//- (void)awakeFromNib //加子控件
(void)setShop:(XMGShop *)shop{
_shop = shop;
// 设置界面图片和文字
self.iconView.image = [UIImage imageNamed:shop.icon];
self.titlelabel.text = shop.name;
}
@end
xib界面的数据bean类:
//
// XMGShop.h
// 03-综合练习
//
// Created by xiaomage on 15/12/29.
// Copyright © 2015年 小码哥. All rights reserved.
//
#import
@interface XMGShop : NSObject
/** 图片的名称 */
@property (nonatomic, copy) NSString *icon;
/** 商品的名称 */
@property (nonatomic, copy) NSString *name;
// 提供构造方法
/*
(instancetype)initWithIcon: (NSString *)icon name: (NSString *)name;
(instancetype)shopWithIcon: (NSString *)icon name: (NSString *)name;
*/
(instancetype)initWithDict:(NSDictionary *)dict;
(instancetype)shopWithDict:(NSDictionary *)dict;
//
// XMGShop.m
// 03-综合练习
//
// Created by xiaomage on 15/12/29.
// Copyright © 2015年 小码哥. All rights reserved.
//
#import "XMGShop.h"
@implementation XMGShop
/*
(instancetype)initWithIcon:(NSString *)icon name:(NSString *)name{
if (self = [super init]) {
self.icon = icon;
self.name = name;
}
return self;
}
(instancetype)shopWithIcon:(NSString *)icon name:(NSString *)name{
return [[self alloc] initWithIcon:icon name:name];
}
*/
(instancetype)initWithDict:(NSDictionary *)dict{
if (self = [super init]) {
self.icon = dict[@"icon"];
self.name = dict[@"name"];
}
return self;
}
(instancetype)shopWithDict:(NSDictionary *)dict{
return [[self alloc] initWithDict:dict];
}
故事板:
手机扫一扫
移动阅读更方便
你可能感兴趣的文章