美文网首页
零碎笔记(一)

零碎笔记(一)

作者: dtythicc | 来源:发表于2017-09-11 12:15 被阅读0次

    暑假两个月没动iOS,做项目时思路清晰却老是忘代码。
    蓝瘦,之前觉得很简单没啥,现在看来我的记性并不好;整理一下顺便再记忆一下,如有不妥还请大佬指教

    1. controller之间上弹下谈式切换

    同push,pop命令用法一样,只不过写法稍不同

    //push
    YourNextViewController *myVC = [[YourNextController alloc]init];
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:myVC];
    [self presentViewController:navi animated:YES completion:nil];
    //pop
    [self dismissViewControllerAnimated:YES completion:nil];
    

    2.weak&strong控件初始化

    将控件声明成strong@property(nonatomic,strong) UIButton *btn;

    那么你在实现这个控件时只需这样:

    _btn = [[UIButton alloc]init];
    [self.view addSubview:_btn]
    

    将控件声明成weak@property(nonatomic,weak) UIButton *btn;

    那么你在实现这个控件时需要这样:

    UIButton *button = [[UIButton alloc]init];
    button.frame = ...
    _btn = button;
    [self.view addSubview:_btn];
    

    3.UI控件边框画线方法(UITextField为例)

    [_answerTV.layer setCornerRadius:4];    //设置边框圆角弯度
    _answerTV.layer.borderWidth = 5; //边框宽度
    _answerTV.layer.borderColor = [[UIColor colorWithRed:0.52 green:0.09 blue:0.07 alpha:0.5] CGColor]; //设置边框颜色
    _answerTV.layer.contents = (id)[UIImage imageNamed:@"TextViewBGC.png"].CGImage; //给图层添加背景图片
    

    4.代理方法

    假定环境:View中的Button点击方法需要ViewController实现

    首先在View.h中声明自定义协议及协议方法

    #import <UIKit/UIKit.h>
    
    @protocol MyViewDelegate <NSObject>
    
    -(void)BtnClicked; //协议方法
    
    @end
    
    @interface View : UIView
    
    @property (nonatomic,assign) id<MyViewDelegate>myDelegate;
    
    @end
    
    

    然后在View.m文件中相应Button点击时调用协议方法

    - (void)BtnClicked:(UIButton *)sender{
        
        [self.myDelegate BtnClicked];
        
    }
    

    然后只需要让ViewController根据协议实现你在View中需要让ViewController实现的就行了

    ViewController.h中声明协议

    #import <UIKit/UIKit.h>
    #import "View.h"
    
    @interface ViewController : UICollectionViewController<myViewDelegate>
    @end
    

    ViewController.m中实现协议方法

    //设置代理
    View *myView = [[View alloc]init];
    myView.myViewDelegate = self;
    
    
    pragma mark <HeaderViewDelegate> 实现代理方法
    -(void)backBtnClicked{ 
        NSLog(@"实现代理方法~");
    }
    

    相关文章

      网友评论

          本文标题:零碎笔记(一)

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