国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

IOS 應(yīng)用程序管理的實(shí)現(xiàn)

2020-07-26 02:40:10
字體:
供稿:網(wǎng)友

IOS 應(yīng)用程序管理的實(shí)現(xiàn)

1. 項(xiàng)目名稱:應(yīng)用管理

2. 項(xiàng)目截圖展示

3. 項(xiàng)目功能

展示應(yīng)用圖標(biāo),名稱和下載按鈕

點(diǎn)擊下載按鈕,出現(xiàn)“正在下載”圖標(biāo)

4. 項(xiàng)目代碼

模型代碼:AppInfo.h

#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@interface AppInfo : NSObject@property (nonatomic, copy) NSString *name;@property (nonatomic, copy) NSString *icon;@property (nonatomic, strong, readonly) UIImage *image;/** 使用字典實(shí)例化模型 */- (instancetype)initWithDict:(NSDictionary *)dict;/** 快速實(shí)例化一個(gè)對象 */+ (instancetype)appInfoWithDict:(NSDictionary *)dict;/** 返回所有plist中的數(shù)據(jù)模型數(shù)組 */+ (NSArray *)appList;@end

模型代碼:AppInfo.m

#import "AppInfo.h"@implementation AppInfo// 合成指令,主動指定屬性使用的成員變量名稱@synthesize image = _image;//圖片模型- (UIImage *)image{  if (_image == nil) {    _image = [UIImage imageNamed:self.icon];  }  return _image;}- (instancetype)initWithDict:(NSDictionary *)dict{  self = [super init];  if (self) {    // 用字典給屬性賦值    //    self.name = dict[@"name"]; //將字典的內(nèi)容賦值給屬性    //    self.icon = dict[@"icon"];    [self setValuesForKeysWithDictionary:dict];  }  return self;}+ (instancetype)appInfoWithDict:(NSDictionary *)dict{  return [[self alloc] initWithDict:dict];}+ (NSArray *)appList{  NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil]];  // 創(chuàng)建一個(gè)臨時(shí)可變數(shù)組  NSMutableArray *arrayM = [NSMutableArray array];  // 遍歷數(shù)組,依次轉(zhuǎn)換模型  for (NSDictionary *dict in array) {    [arrayM addObject:[AppInfo appInfoWithDict:dict]];  }  return arrayM;}@end

模型View:AppView.h

#import <UIKit/UIKit.h>@class AppInfo;@interface AppView : UIView/** 類方法,方便調(diào)用視圖 */+ (instancetype)appView;/** 實(shí)例化視圖,并使用appInfo設(shè)置視圖的顯示 */+ (instancetype)appViewWithAppInfo:(AppInfo *)appInfo;// 自定義視圖中顯示的數(shù)據(jù)來源是數(shù)據(jù)模型// 使用模型設(shè)置自定義視圖的顯示@property (nonatomic, strong) AppInfo *appInfo;@end

模型View:AppView.m

#import "AppView.h"#import "AppInfo.h"@interface AppView()@property (weak, nonatomic) IBOutlet UIImageView *iconView;@property (weak, nonatomic) IBOutlet UILabel *label;@end@implementation AppView//實(shí)例化xib+ (instancetype)appView{  return [[[NSBundle mainBundle] loadNibNamed:@"AppView" owner:nil options:nil] lastObject];}//根據(jù)模型實(shí)例化xib+ (instancetype)appViewWithAppInfo:(AppInfo *)appInfo{  // 1. 實(shí)例化一個(gè)視圖  AppView *view = [self appView];  // 2. 設(shè)置視圖的顯示  view.appInfo = appInfo;//包含,AppView有appInfo的屬性  // 3. 返回視圖  return view;}/** 利用setter方法設(shè)置視圖的界面顯示 */- (void)setAppInfo:(AppInfo *)appInfo{  _appInfo = appInfo;  self.label.text = appInfo.name;  self.iconView.image = appInfo.image;}/** 按鈕監(jiān)聽方法 */- (IBAction)click:(UIButton *)button{  // 添加一個(gè)UILabel到界面上  UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(80, 400, 160, 40)];  // 數(shù)值是0表示黑色,1表示純白;alpha表示透明度  label.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.2];  label.text = self.appInfo.name;  label.textAlignment = NSTextAlignmentCenter;  // self.superview就是視圖控制器中的self.view  [self.superview addSubview:label];  // 動畫效果  label.alpha = 0.0;  // 禁用按鈕,如果點(diǎn)擊了按鈕以后就禁用按鈕  button.enabled = NO;  // 動畫結(jié)束之后刪除  [UIView animateWithDuration:1.0f animations:^{    // 要修改的動畫屬性    label.alpha = 1.0;  } completion:^(BOOL finished) {    [UIView animateWithDuration:1.0 animations:^{      label.alpha = 0.0;    } completion:^(BOOL finished)       [label removeFromSuperview];    }];  }];}@end

ViewController.m

#import "ViewController.h"#import "AppInfo.h"#import "AppView.h"#define kAppViewW 80#define kAppViewH 90#define kColCount 3#define kStartY  20@interface ViewController ()/** 應(yīng)用程序列表 */@property (nonatomic, strong) NSArray *appList;@end@implementation ViewController- (NSArray *)appList{  if (_appList == nil) {    _appList = [AppInfo appList];  }  return _appList;}- (void)viewDidLoad{  [super viewDidLoad];  // 搭建九宮格  // 320 - 3 * 80 = 80 / 4 = 20  CGFloat marginX = (self.view.bounds.size.width - kColCount * kAppViewW) / (kColCount + 1);  CGFloat marginY = 10;  for (int i = 0; i < self.appList.count; i++) {    // 行    int row = i / kColCount;    // 列    int col = i % kColCount;    CGFloat x = marginX + col * (marginX + kAppViewW);    CGFloat y = kStartY + marginY + row * (marginY + kAppViewH);    //加載第i個(gè)xib視圖    AppView *appView = [AppView appViewWithAppInfo:self.appList[i]];    // 設(shè)置視圖位置    appView.frame = CGRectMake(x, y, kAppViewW, kAppViewH);    [self.view addSubview:appView];    }}

5. 本項(xiàng)目必須掌握的代碼段

字典轉(zhuǎn)模型

- (instancetype)initWithDict:(NSDictionary *)dict{  self = [super init];  if (self) {    [self setValuesForKeysWithDictionary:dict];  }  return self;}+ (instancetype)appInfoWithDict:(NSDictionary *)dict{  return [[self alloc] initWithDict:dict];}+ (NSArray *)appList{  NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil]];  // 創(chuàng)建一個(gè)臨時(shí)可變數(shù)組  NSMutableArray *arrayM = [NSMutableArray array];  // 遍歷數(shù)組,依次轉(zhuǎn)換模型  for (NSDictionary *dict in array) {    [arrayM addObject:[AppInfo appInfoWithDict:dict]];  }  return arrayM;}

KVC

 [self setValuesForKeysWithDictionary:dict];

6. 筆記

字典轉(zhuǎn)模型:

plist文件有多個(gè)字典,把字典的元素轉(zhuǎn)換成模型類對象的成員變量,將模型類對象放入數(shù)組中 模型的屬性名稱和plist文件中的key名稱必須一致

如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 舞阳县| 湘西| 江达县| 新巴尔虎左旗| 十堰市| 宁阳县| 莎车县| 开封县| 广水市| 清河县| 蒙阴县| 东至县| 辉县市| 醴陵市| 绩溪县| 屯门区| 浦北县| 鲜城| 华宁县| 遵义县| 舞钢市| 大埔县| 宝山区| 泸西县| 穆棱市| 巴中市| 西林县| 封开县| 林州市| 华安县| 中山市| 苍南县| 喀喇| 南和县| 拉萨市| 商城县| 鄂伦春自治旗| 华亭县| 平度市| 阿拉尔市| 蕲春县|