暑假兩個(gè)月沒動(dòng)iOS,做項(xiàng)目時(shí)思路清晰卻老是忘代碼。
藍(lán)瘦,之前覺得很簡單沒啥,現(xiàn)在看來我的記性并不好;整理一下順便再記憶一下,如有不妥還請(qǐng)大佬指教
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;
那么你在實(shí)現(xiàn)這個(gè)控件時(shí)只需這樣:
_btn = [[UIButton alloc]init];
[self.view addSubview:_btn]
將控件聲明成weak@property(nonatomic,weak) UIButton *btn;
那么你在實(shí)現(xiàn)這個(gè)控件時(shí)需要這樣:
UIButton *button = [[UIButton alloc]init];
button.frame = ...
_btn = button;
[self.view addSubview:_btn];
3.UI控件邊框畫線方法(UITextField為例)
[_answerTV.layer setCornerRadius:4]; //設(shè)置邊框圓角彎度
_answerTV.layer.borderWidth = 5; //邊框?qū)挾?_answerTV.layer.borderColor = [[UIColor colorWithRed:0.52 green:0.09 blue:0.07 alpha:0.5] CGColor]; //設(shè)置邊框顏色
_answerTV.layer.contents = (id)[UIImage imageNamed:@"TextViewBGC.png"].CGImage; //給圖層添加背景圖片
4.代理方法
假定環(huán)境:View中的Button點(diǎn)擊方法需要ViewController實(shí)現(xiàn)
首先在View.h中聲明自定義協(xié)議及協(xié)議方法
#import <UIKit/UIKit.h>
@protocol MyViewDelegate <NSObject>
-(void)BtnClicked; //協(xié)議方法
@end
@interface View : UIView
@property (nonatomic,assign) id<MyViewDelegate>myDelegate;
@end
然后在View.m文件中相應(yīng)Button點(diǎn)擊時(shí)調(diào)用協(xié)議方法
- (void)BtnClicked:(UIButton *)sender{
[self.myDelegate BtnClicked];
}
然后只需要讓ViewController根據(jù)協(xié)議實(shí)現(xiàn)你在View中需要讓ViewController實(shí)現(xiàn)的就行了
ViewController.h中聲明協(xié)議
#import <UIKit/UIKit.h>
#import "View.h"
@interface ViewController : UICollectionViewController<myViewDelegate>
@end
ViewController.m中實(shí)現(xiàn)協(xié)議方法
//設(shè)置代理
View *myView = [[View alloc]init];
myView.myViewDelegate = self;
pragma mark <HeaderViewDelegate> 實(shí)現(xiàn)代理方法
-(void)backBtnClicked{
NSLog(@"實(shí)現(xiàn)代理方法~");
}