美文网首页
iOS - 项目开发相关

iOS - 项目开发相关

作者: 风之化身呀 | 来源:发表于2020-04-09 09:23 被阅读0次
    • 目录结构

    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 - 项目开发相关

          本文链接:https://www.haomeiwen.com/subject/vvxgphtx.html