iOS MVC简单使用

2015-05-28  本文已影响155人  雪波

创建数据模型

/** 图片名 */
@property (strong, nonatomic) NSString *icon;

/** 商品名 */
@property (strong, nonatomic) NSString *name;
- (instancetype)initWithDict:(NSDictionary *)dict{

    if (self = [super init]) {
        self.icon = dict[@"icon"];
        self.name = dict[@"name"];
    }
    return self;
}
/** 懒加载shops内容 */
- (NSArray *)shops{
    if (_shops == nil){
        NSString *file = [[NSBundle mainBundle] pathForResource:@"shops" ofType:@"plist"];
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:file];
        NSMutableArray *shopsArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray){
            XBShop *shop = [XBShop shopWithDict:dict];
            [shopsArray addObject:shop];
        }
        _shops = shopsArray;
    }
    return _shops;
}

自定义控件

/** 商品模型 */
@property (strong, nonatomic) XBShop *shop;
@interface XBShopView ()

@property(strong, nonatomic)UIImageView *shopIcon;
@property(strong, nonatomic)UILabel *nameLabel;

@end
- (void)setShop:(XBShop *)shop{
    _shop = shop;
    self.shopIcon.image = [UIImage imageNamed:shop.icon];
    self.nameLabel.text = shop.name;
}

- 添加新控件

HUD##

    self.hud.alpha = 1.0;   // 设置透明度为1.0可见
    self.hud.text = text;   // 设置文本内容

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.hud.alpha = 0.0;   // 1.5s 后设置透明度为0.0消失
    });

定时任务

// 1.5s后自动调用self的hideHUD方法
[self performSelector:@selector(hideHUD) withObject:nil afterDelay:1.5];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    // 1.5s后自动执行这个block里面的代码
    self.hud.alpha = 0.0;
});
// 1.5s后自动调用self的hideHUD方法
[NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(hideHUD) userInfo:nil repeats:NO];
// repeats如果为YES,意味着每隔1.5s都会调用一次self的hidHUD方法
上一篇 下一篇

猜你喜欢

热点阅读