iOS - 項目開發相關

  • 目錄結構

1.主目錄按照業務分類,內目錄按照模塊分類
2.主目錄按照模塊分類,內目錄按照業務分類(推薦)


  • 常用第三方庫

1、網絡請求 AFNetworking
2、圖片下載與緩存 SDWebImage
3、JSON 格式化 JSONModel
4、Flex 布局庫 Yoga
5、UICollectionView 框架 IGListKit
6、日志庫 CocoaLumberjack

  • 本地存數據 NSUserDefaults
//保存NSInteger
[defaults setInteger:(NSInteger) forKey:(nonnull NSString *)];
//保存BOOL
[defaults setBool:(BOOL) forKey:(nonnull NSString *)];
// 保存對象
[defaults setObject:@"用戶名" forKey:kUsernameKey];

//讀取NSInteger
[defaults setInteger:(NSInteger) forKey:(nonnull NSString *)];
//讀取BOOL
[defaults setBool:(BOOL) forKey:(nonnull NSString *)];
// 讀取對象
NSString *username = [defaults objectForKey:kUsernameKey];

//刪除指定key的數據
[defaults removeObjectForKey:(nonnull NSString *)];
  • 基本
[UIScreen mainScreen].bounds.size  //  屏幕寬高
[UIColor redColor]  // 顏色
  • UILabel 使用
_titleLabel = [[UILabel alloc] init];
_titleLabel.text=@"歡迎使用";
_titleLabel.textColor = [UIColor blackColor];
_titleLabel.font = [UIFont systemFontOfSize:23];

// 第二種方式
UILabel *label = [UILabel new];
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] 
                                   initWithString:@"127"];
[text addAttribute:NSKernAttributeName 
             value:@-0.5 
             range:NSMakeRange(0, text.length)];
[label setAttributedText:text];
  • UIImage & UIImageView 使用
// 圖片要放到 Assets.xcassets 里
UIImage *image= [UIImage imageNamed:@"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
  • UIButton 使用
    內部有 titleLabel,titleImage 等屬性
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.backgroundColor = [UIColor clearColor];
[button1 setImage:[UIImage imageNamed:@"btng.png"] forState:UIControlStateNormal];
_button.layer setMasksToBounds:YES];
[_button.layer setCornerRadius:10.0]; //設置矩形四個圓角半徑
[button1 setTitle:@"點擊" forState:UIControlStateNormal];
[button1.titleLabel setFont:[UIFont systemFontOfSize:40]];
[button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
  • UITableView

1、Controller需要實現兩個 delegate ,分別是 UITableViewDelegate 和 UITableViewDataSource

// GTNewsViewController.m
@interface GTNewsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong, readwrite) UITableView *tableView;
@property (nonatomic, strong, readwrite) NSArray *dataArray;
@end

2、然后 UITableView對象的 delegate要設置為 self

_tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];

3、然后就可以實現這些delegate的一些方法。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 10;
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;              
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;   
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

// 返回指定的row 的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * showUserInfoCellIdentifier = @"ShowUserInfoCell";
    UITableViewCell * cell = [tableView_ dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];
    if (cell == nil){
        // Create a cell to display an ingredient.
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                       reuseIdentifier:showUserInfoCellIdentifier] 
                autorelease];
    }
    // Configure the cell.
    cell.textLabel.text=@"簽名";
    cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str()  encoding:NSUTF8StringEncoding];
        }

// 響應行點擊
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        return;
    }
    else if(indexPath.section==0){
        switch (indexPath.row) {
            //聊天
            case 0:{
                [self  onTalkToFriendBtn];
            }
                break;
            default:
                break;
        }
    }
    else {
        return ;
    }
}
  • YogaKit 布局

以下兩句容易忘記
1、layout.isEnabled =YES
2、[self.view.yoga applyLayoutPreservingOrigin:NO]

// 父級 view 設置垂直居中
    self.view.backgroundColor = [UIColor greenColor];
    [self.view configureLayoutWithBlock:^(YGLayout *_Nonnull layout) {
        layout.isEnabled = YES;
        layout.alignItems = YGAlignCenter;
        layout.justifyContent = YGJustifyCenter;
    }];

    // 子view
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor redColor];
    [view configureLayoutWithBlock:^(YGLayout *layout) {
        layout.isEnabled = YES;
        layout.width = YGPointValue(100);
        layout.height = YGPointValue(100);
        layout.flexDirection = YGFlexDirectionRow;
        layout.alignItems = YGAlignCenter;
        layout.justifyContent = YGJustifyCenter;
    }];
    [self.view addSubview:view];

    // 嵌套子 view
    [view addSubview:({
        UIView *view = [[UIView alloc] init];
        [view configureLayoutWithBlock:^(YGLayout *_Nonnull layout) {
            layout.isEnabled = YES;
            layout.width = YGPointValue(20);
            layout.height = YGPointValue(20);
        }];
        view.backgroundColor = [UIColor blackColor];
        view;
    })];

    [self.view.yoga applyLayoutPreservingOrigin:NO];

一些問題

  • LaunchScreen.storyboard & Main.storyboard 的區別
    注意:應用啟動也是需要時間的。
    LaunchScreen 是用戶點擊啟動圖標后展示的第一個頁面,這時候應用還沒完成啟動,這里不能有任何用戶代碼
    Main 是應用完成啟動后展示的第一個頁面,可以有用戶代碼
    從展示順序來看,先 LaunchScreen 再 Main
  • Xcode11添加引導頁(升級后Launch Images Source選項不見了)
    Xcode11添加引導頁(升級后Launch Images Source選項不見了)
  • 第一次用 Podfile 安裝第三方依賴后,需要點擊 .xcworkspace 重新打開項目。注意不再是 .xcodeproj
  • 快捷鍵
    1.運行:command + R
    2.編譯:command + B
    3.停止:command + .
    4.工程導航如圖從左到右分別對應 command +1~8.
    5.快速打開/隱藏右上角實用面板
    a.command + 0 (注意是“0”不是“o”)
    b.command+option+0(zero)
    c.command + shift + Y



    6.快速查找打開類:command+ shift+ O
    7.刪除一行 command+delete

  • 屬性和成員變量在.h文件和.m文件區別

在.h文件中聲明的屬性,外部類可以通過“類實例.屬性”來調用,
但在.m中聲明的則不可以,獲取和設置的方法,只能是通過setValue:forKey和valueForKey來實現。屬于私有的,子類不可訪問。

  • 屬性定義的關鍵詞

atomatic 讀寫安全,線程不安全
nonatomatic 速度快
strong 強引用
copy 深度復制一份
weak 若引用
readonly 只讀
readwrite 可讀可寫
更多

  • MVC 結構

1、Model 寫法

//.h聲明
#import <Foundation/Foundation.h>
//第一個model
@interface NewModel : NSObject
@property(nonatomic,copy)NSString *familyName;//姓氏
@property(nonatomic,strong)NSArray *messageArray;//信息
- (instancetype)initWithDic:(NSDictionary *)dic;
@end

//第二個model
@interface NewModel2 : NSObject
@property(nonatomic,copy)NSString *name;//姓名
@property(nonatomic,copy)NSString *sex;//性別
- (instancetype)initWithDic:(NSDictionary *)dic;
@end

//.m實現
#import "NewModel.h"
// 第一個model
@implementation NewModel
/**
 *  構造
 *
 *  @param dic <#dic description#>
 *
 *  @return <#return value description#>
 */
- (instancetype)initWithDic:(NSDictionary *)dic
{
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dic];
        //創建一個可變數組加載soldarray
        NSMutableArray *newArray = [NSMutableArray array];
        for (NSDictionary *dic in self.messageArray) {
            NewModel2 *model = [[NewModel2 alloc]initWithDic:dic];
            [newArray addObject:model];
        }
        self.messageArray = newArray;
    }
    return self;
}
@end

//第二個model
@implementation NewModel2
/**
 *  構造
 *
 *  @param dic <#dic description#>
 *
 *  @return <#return value description#>
 */
- (instancetype)initWithDic:(NSDictionary *)dic
{
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dic];
    }
    return self;
}
@end

2、View 寫法

// example.m
@interface GTDeleteCellView ()
@property (nonatomic, strong, readwrite) UIView *backgroundView;
@property (nonatomic, strong, readwrite) UIButton *deleteButton;
@property (nonatomic, copy, readwrite) dispatch_block_t deleteBlock;
@end

@implementation GTDeleteCellView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:({
            _backgroundView = [[UIView alloc] initWithFrame:self.bounds];
            _backgroundView.backgroundColor = [UIColor blackColor];
            _backgroundView.alpha  = 0.5;
            [_backgroundView addGestureRecognizer:({
                UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDeleteView)];
                tapGesture;
            })];
            _backgroundView;
        })];
        self.clipsToBounds = YES;
    }
    return self;
}
@end

注意:定義的私有屬性可通過 _ 的形式訪問
3、Controller 寫法

//.h聲明
#import <UIKit/UIKit.h>

@interface NewViewController : UIViewController
@property(nonatomic,weak)UITableView *tableView;
@property(nonatomic,strong)NSArray *foldArray;//數據
@end

//.m實現
#import "NewViewController.h"
#import "NewModel.h"
@interface NewViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation NewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //加載表
    [self loadTableView];
}

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

/**
 *  數據加載
 */
-(NSArray *)foldArray
{
    if (_foldArray == nil) {
        NSString *path = [[NSBundle mainBundle]pathForResource:@"data.plist" ofType:nil];
        NSArray *oldArray = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *newArray = [NSMutableArray array];
        for (NSDictionary *dic in oldArray) {
            NewModel *model = [[NewModel alloc]initWithDic:dic];
            [newArray addObject:model];
        }
        _foldArray = newArray;
    }
    return _foldArray;
}
/**
 *  加載表的方法
 */
- (void)loadTableView
{
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    self.tableView = tableView;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.foldArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NewModel *fModel = self.foldArray[section];
    return fModel.messageArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * const ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    NewModel *fModel = self.foldArray[indexPath.section];
    NewModel2 *sModel = fModel.messageArray[indexPath.row];
    cell.textLabel.text = sModel.name;
    cell.detailTextLabel.text = sModel.sex;
    return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NewModel *fModel = self.foldArray[section];
    return fModel.familyName;
}
@end
  • 某 .a 文件找不到


    image.png
  • 某 .h 文件找不到


    image.png

    解決方式基本一樣,就是在Build Settings 里設置 library search path 或者 framework search path


    image.png
  • xcode 的 scheme 文件可以改變構建 Debug 還是 Release
  • xcode 的 product-archive 可以用來打包 ipa
  • 使用 storyboard 方式,系統會自動創建一個UIWindow 窗口,并以ViewController.m 作為初始View。這也是 Xcode 新建項目的默認配置
  • 不使用 storyboard 方式則需要自己在 AppDelegate.m 的 didFinishLaunchWithOptions 方法中創建 UIWindow
  • Xcode-Xcode extensions - xcformat 格式化工具
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,412評論 6 532
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,514評論 3 416
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,373評論 0 374
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,975評論 1 312
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,743評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,199評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,262評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,414評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,951評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,780評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,983評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,527評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,218評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,649評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,889評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,673評論 3 391
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,967評論 2 374

推薦閱讀更多精彩內容