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 格式化工具
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • iOS 實戰開發課程筆記 本貼旨在作為對極客班 《iOS 開發實戰》第五期期課程視頻重新學習的筆記。目標是建立一個...
    黃穆斌閱讀 3,105評論 12 57
  • 1,NSObject中description屬性的意義,它可以重寫嗎?答案:每當 NSLog(@"")函數中出現 ...
    eightzg閱讀 4,192評論 2 19
  • 基礎 1. 為什么說Objective-C是一門動態的語言? 2. 講一下MVC和MVVM,MVP? 3. 為...
    波妞和醬豆子閱讀 3,361評論 0 46
  • 無星的夜空 無魚的湖 無鳥的藍空 好空,好空,沒心了吧
    花開了吧閱讀 94評論 0 0
  • 從2016.3.20開始的事不知道是喜劇還是悲劇,喜劇也好,悲劇也好,還是走到了今天。一場不以“愛”開始的愛情,開...
    產品小白閱讀 265評論 0 0