0814-應用程序管理(筆記)



#import "ViewController.h"
#import "AppInfo.h"
@interface ViewController ()
@property (nonatomic,strong) NSArray *appList;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //搭建界面,九宮格
//    UIView *view1=[[UIView alloc] initWithFrame:CGRectMake(20, 20, 80, 90)];
//    view1.backgroundColor=[UIColor redColor];
//    [self.view addSubview:view1];
//    
//    UIView *view2=[[UIView alloc] initWithFrame:CGRectMake(120, 20, 80, 90)];
//    view2.backgroundColor=[UIColor redColor];
//    [self.view addSubview:view2];
//    
//    UIView *view3=[[UIView alloc] initWithFrame:CGRectMake(220, 20, 80, 90)];
//    view3.backgroundColor=[UIColor redColor];
//    [self.view addSubview:view3];

#define  KAppViewW 80
#define  KAppViewH 90
#define  KColCount 3
    //option 可以在storyboard複製
   // ios7在原點  ios6在狀態欄下面
#define  KStartY 20



    //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<12; i++) {

        //行 0,1,2=>0 3,4,5=>1
        int row=i/KColCount;
        //列 0,3,6=>0 1,4,7=>1
        int col=i%KColCount;
        CGFloat x=marginX+col*(marginX+KAppViewW);
        CGFloat y=KStartY+marginY+row*(marginY+KAppViewH);
        UIView *appView=[[UIView alloc] initWithFrame:CGRectMake(x, y, KAppViewW, KAppViewH)];

        [self.view addSubview:appView];

        //實現視圖內部細節
    //1>UIImageView
        UIImageView *icon=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, KAppViewW, 50)];

        icon.image=[UIImage imageNamed:dict[@"icon"]];
        //設置圖像填充模式,等比例顯示(CTRL+6)
        icon.contentMode=UIViewContentModeScaleAspectFit;
        [appView addSubview:icon];

        //2>UILabel->應用程序名稱
        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(icon.frame), KAppViewW, 20)];
        label.text=dict[@"name"];
        //設置字體
        label.font=[UIFont systemFontOfSize:13.0];
        label.textAlignment=NSTextAlignmentCenter;
        [appView addSubview:label];
        //3>UIButton-->下載按鈕
        UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(label.frame), KAppViewW, 20)];
        //背景圖片
//        button.backgroundColor=[UIColor yellowColor];
        [button setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted];

        //按鈕都是有狀態的,不同狀態可以對應不同的標題
        [button setTitle:@"下載" forState:UIControlStateNormal];
        //***一定不要使用以下方法,修改按鈕標題
//        button.titleLabel.text=@"aaa"; 沒有顯示
        //提示:按鈕的字體是不區分狀態的!

        //修改字體(titleLabel是隻讀的)
//        readOnly表示不允許修改titleLabel的指針,但是可以修改label的字體
        button.titleLabel.font=[UIFont systemFontOfSize:12.0];

        [appView addSubview:button];

        //CGRectGetMaxY(frame)=frame.origin.y+frame.size.height;


    }

 }
-(NSArray *)appList
{
    if(_appList==nil){
        _appList=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil]];


    }
       return _appList;

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

字典轉模型

   //AppList保存字典模型
        NSArray *array=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil]];
        //創建一個臨時數組
        NSMutableArray *arrayM=[NSMutableArray array];
        //遍歷數組,依次轉換模型
        for(NSDictionary *dict in array ){
            AppInfo *appInfo=[[AppInfo alloc] init];
            appInfo.name=dict[@"name"];
            appInfo.icon=dict[@"icon"];

            [arrayM addObject:appInfo];

        }
        //將臨時數組的屬性爲其賦值
        _appList=arrayM;

取:

    for (int i=0; i<12; i++) {
        NSDictionary *dict=self.appList[i]; 
        AppInfo *appInfo=self.appList[i];
        NSLog(@"%@===",appInfo.name);

介紹幾個xcode插件
提供了圖像名稱填寫的自動化
顏色更加可視化
Reveal作爲分析APP UI的利器
VVDocumenter Xcode快速添加註釋插件

快捷鍵
ctrl+shift+o 顯示源代碼
commoand +6 顯示方法或者在上面選擇
esc鍵提示

xcode代碼塊語法
這裏寫圖片描述

id和instanceType區別

.h

/**通常在寫模型實例化方法時,以下兩個方法,都需要實現*/
/**使用字典實例化模型*/
-(id)initWithDict:(NSDictionary *)dict;
/**類方法可以快速實例化一個對象*/
+(id) appInfoWithDict:(NSDictionaty *)dict;

.m

-(id) initWithDict:(NSDictionary *)dict
{
  self=[super init];
if(self){
//用字典給屬性賦值
self.name=dict[@"name"];
self.icon=dict[@"icon"];
}
//self是對象
return self;
}


+(id) appInfoWithDict:(NSDictionary *)dict{
//self是class
return [[self alloc] initWithDict:dict];
}

調用

for(NSDictionary *dict in array){

//類方法可以快速實例化一個對象
 AppInfo *appInfo=[[AppInfo alloc] initWithDict:dict];
//就會有自動提示
[AppInod appInfoWithDict:dict];

}

id換成instanceType

instanttype主要用於在類方法實例化對象時,讓編輯器主動推斷對象的實際類型
**以避免使用id,會照成開發中不必要的麻煩,減少出錯機率.
instanttype是蘋果ios7纔開始主推的

C++11 auto
在Swift語言中,絕大多數類的實例化,都不需要再指定類型

instancetype 只能用於返回值使用!!!不能當做參數使用,(id可以)

KVC-key value coding鍵值編碼:是一種間接修改/讀取對象屬性的一種方法
self.name=dict[@"name"];
self.icon=dict[@"icon"];
參數1.數值 2.屬性名稱
[self setValue:dict[@"name"]  forKeyPath:@"name"];
[self setValue:dict[@"icon"]  forKeyPath:@"icon"];
或者
[self setValuesForKeysWithDictionary:dict];

錯誤提示:[UIImage imageNamed:nil];
CUiCatalog:Invalid asset name supplied:(null),or invalid scale factor:2.000000

setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key name 
類 setValue 方法,沒有匹配的鍵值

使用KVC的注意事項
1.plist中的鍵值名稱必須與模型中一致
2模型中的屬性可以不全部出現在plist中

-(UIImage *)image {
if(_image ==nil){
_image =[UIImage imageNamed:self.icon];
}
return _image;
}

/**
@property
1.生成getter方法
2.生成setter方法
3.生成帶下劃線的成員變量(記錄屬性內容)
readOnly的屬性不會生成帶下劃線的成員變量
*/
@property(nonatomic,strong,readonly) UIImage *image;
會報錯

readOnly還有set方法
-(void)setImage:(UIImage *)image{ }
但是下劃線成員變量沒有了 


合成指令(xcode 4.1的老代碼)
@synthesize image=_image;
//主動指定屬性使用的成員變量名稱
這樣就在readOnly也可以生成成員變量

/**返回所有plist中的數據模型數組*/
+(NSArray *)appList;

+(NSArray *)appList{
return arrayM;
}


調用 [AppInfo appList];

/* 應用程序列表 /
@property (nonatomic, strong) NSArray *appList;
for (int i = 0; i < self.appList.count; i++) {
appList.count獲得數組的個數

動畫監聽

[self.view addSubview:label];

    //增加動畫效果 首尾式動畫,修改對象的屬性:frame,bounds,alpha 初始透明度

     label.alpha=0.0;
//    [UIView beginAnimations:nil context:nil];
//    [UIView  setAnimationDuration:1.0f];
//    label.alpha=1.0;
//    [UIView commitAnimations];
    // 缺點:不容易監聽動畫完成時間,而且不容易實現動畫嵌套.


    //禁用按鈕
    button.enabled=NO;


    //換一種方式

    //動畫結束之後刪除
    [UIView animateWithDuration:1.0f animations:^{
        NSLog(@"動畫開始");
        //要修改的動畫屬性
        label.alpha=1.0;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1.0 animations:^{
            label.alpha=0.0;
        } completion:^(BOOL finished) {
            //動畫完成後,所做的操作
            NSLog(@"動畫完成");
            [label removeFromSuperview];
        }];
        NSLog(@"_____");

        /*
         2016-01-15 09:45:17.996 01-應用程序管理[1427:28852] 動畫開始
         2016-01-15 09:45:19.000 01-應用程序管理[1427:28852] _____
         2016-01-15 09:45:20.005 01-應用程序管理[1427:28852] 動畫完成
         */

        //^表示的是block 塊代碼,是一個預先準備好的代碼塊.,可以當做參數傳遞,在需要的時候執行!
    }];

XIB

1.storyBoard 重量級,能夠描述一個應用程序所有的界面
2.XIB輕量級,在Xcode4.0之前,是主要的圖像界面搭建工具,在現在,仍然是主流的界面開發技術,適合於開發小塊的自定義視圖.

- (void)viewDidLoad
{
    [super viewDidLoad];



    //XIB的測試代碼
    //加載XIB XIB中可以包含多個自定義視圖,通常只保存一個
    NSArray *array=[[NSBundle mainBundle] loadNibNamed:@"AppView" owner:nil options:nil];

    UIView *firstview=    [array firstObject];
    UIView *lastview=    [array lastObject];



    NSLog(@"%@",array);

    /*2016-01-17 12:34:14.393 01-應用程序管理[3495:110074] (
     "<UIView: 0x7f934cb04fb0; frame = (0 0; 80 90); autoresize = RM+BM; layer = <CALayer: 0x7f934cb001c0>>",
     "<UISegmentedControl: 0x7f934b43f030; frame = (0 0; 121 29); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x7f934b43f450>>",
     "<UITextField: 0x7f934c80b090; frame = (0 0; 97 30); text = ''; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x7f934c80bdd0>>",
     "<UITableView: 0x7f934c01b200; frame = (0 0; 600 600); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7f934ca0f4c0>; layer = <CALayer: 0x7f934ca087e0>; contentOffset: {0, 0}; contentSize: {600, 0}>"
     )*/

    //從XIB來加載自定義視圖
       UIView *view=[[[NSBundle mainBundle] loadNibNamed:@"HMAppView" owner:nil options:nil] lastObject];
    //設置視圖位置
    view.frame=CGRectMake(x, y, kAppViewH, kAppViewW);

    [self.view addSubview:view];

//    image+label+button
    //實現視圖內部細節
    AppInfo *appInfo=self.appList[i];

    //UIImageView
    UIImageView *icon=appInfo.subviews[0];

    //設置圖像 其他的圖片屬性可以直接設置,也不要設置subView
    icon.image=appInfo.imagel;

自定義視圖

使得一個xib文件和custom class關聯一個.m和.h文件,並做連線
在要使用的控件裏面導入#import “AppView.h”
然後上面的UIImageView *icon=appview.subView[0];就可以改成
UIImgaeView *icon=appView.iconView;
這樣就不用擔心[0,1,2]寫死的下標了.

開發小結

  1. 運行示例程序,確定開發思路
  2. 搭建界面,編寫代碼
  3. 九宮格的佈局
  4. 字典轉模型,在iOS開發中,非常重要
    /* 使用字典實例化模型 /
    • (instancetype)initWithDict:(NSDictionary *)dict;
      /* 類方法可以快速實例化一個對象 /
    • (instancetype)appInfoWithDict:(NSDictionary *)dict;

// KVC賦值
[self setValuesForKeysWithDictionary:dict];

  1. 實現了按鈕的監聽方法
  2. 導入XIB,簡化代碼搭建界面
  3. 自定義視圖,使用模型設置視圖的內容

重構:
原則:讓代碼放在最應該存在的地方
方法:
1. 設定開發計劃,開發步驟
2. “每一個步驟告一段落後,需要暫停,審覈代碼”,有針對性的重構!

目前要做到的,儘量不要出現重複的代碼!

====================================================================================
Reveal使用
1. Build Settings 搜索Other
將Other Linker Flags 設置爲 -ObjC
2. Reveal.framework拖到項目中即可

~/資源庫/Caches/
找到com.ittybittyapps.Reveal文件夾刪除
~/資源庫/Preferences/
找到com.ittybittyapps.Reveal.plist刪除
又可以使用30天

這裏寫圖片描述

apple文檔 & view Controller Programming Guide

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章