有時(shí)間把平時(shí)用到的知識(shí)點(diǎn)總結(jié)下肮之。下次遇見(jiàn)就不用再查各種資料啦
1.kvo 傳值
b—>a在b的.h文件中,定義變量
@property (nonatomic, copy) NSString *string;
然后在需要傳值的地方進(jìn)行傳值
-(void)buttonAction:(UIButton*)sender{
//KVO ? 把bTextField.text賦值給當(dāng)前屬性挡逼,在需要接收的地方監(jiān)聽(tīng)當(dāng)前屬性
self.string = bTextField.text;}
在.a 中
/*
KVO創(chuàng)建。三步一定要寫(xiě)
1.注冊(cè)觀察者
2.KVO回調(diào)
3.移除觀察者
*/
[bViewController addObserver:self forKeyPath:@"string" options:NSKeyValueObservingOptionNew context:nil];
2.通知傳值
通知//通知傳值 一般是用于回傳//現(xiàn)在是A傳到B //發(fā)送通知的方法 要寫(xiě)在執(zhí)行方法里面
[[NSNotificationCenter defaultCenter]postNotificationName:@"tz" object:niluserInfo:@{@"key":aTextField.text}];
//如果是從A傳到B的話(huà),B.m里要?jiǎng)?chuàng)建一個(gè)init方法,在里面寫(xiě)監(jiān)聽(tīng)并在里面創(chuàng)建接收容器才能成功(因?yàn)槌绦蛳葓?zhí)行init方法再到viewDidLoad方法,當(dāng)傳值過(guò)去時(shí)在init就開(kāi)始監(jiān)聽(tīng),如果這里沒(méi)有創(chuàng)建textField接收,那就傳不過(guò)去了,所以要在init里同時(shí)創(chuàng)建接收器(生命周期的問(wèn)題));
-(instancetype)init
{
if (self =[super init]) {
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(tzAction:) name:@"tz" object:nil];
bTextField =
[[UITextField alloc] initWithFrame:CGRectMake([UIScreen
mainScreen].bounds.size.width/2-50, 200, 100, 30)];
bTextField.layer.borderColor = [UIColor grayColor].CGColor;
bTextField.layer.borderWidth = 1;
[self.viewaddSubview:bTextField];
}return self;
}
- (void)tzAction:(NSNotification *)sender {
NSLog(@"aaa");
bTextField.text = sender.userInfo[@"key"];
}
移除通知
-(void)dealloc{
//移除所有的通知
[[NSNotificationCenter defaultCenter]removeObserver:self];
//移除某個(gè)通知
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"tz" object:nil];
}
3.tableview設(shè)置footer
UIView *view = [[UIView alloc]init];
view.backgroundColor = [UIColor groupTableViewBackgroundColor];self.tableView.tableFooterView = view;
4.數(shù)組倒序
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",nil];
NSArray*?reversedArray?=?[[arrayreverseObjectEnumerator]?allObjects];
5.App跳轉(zhuǎn)至系統(tǒng)Settings
跳轉(zhuǎn)在IOS8以上跟以下是有區(qū)別的,如果是IOS8以上可以如下設(shè)置:
NSURL *url = [NSURLURLWithString:UIApplicationOpenSettingsURLString];
if([[UIApplication sharedApplication]canOpenURL:url]) {
[[UIApplicationsharedApplication] openURL:url];
}
6.根據(jù)indexPath找到對(duì)應(yīng)的cell
NSIndexPath *indexPath =[NSIndexPath indexPathForRow:3 inSection:0];
UITableViewCell*cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.detailTextLabel.text= [self.dateFormatterstringFromDate:self.pickerView.date];
7.定義一個(gè)簡(jiǎn)單的block傳值
? ? 1.在需要傳值的.h 文件中定義一個(gè)block
@property(strong,nonatomic)void(^calendarBlock)(NSDate* selectdate);
? 2.在需要傳值的邀窃。M文件中,比如一個(gè)按鈕的點(diǎn)擊事件之類(lèi)的地方進(jìn)行判斷假哎,例如
- (IBAction)previousAction:(UIButton*)sender {
self.date= [selflastDay:self.date];
if(self.calendarBlock) {
self.calendarBlock(self.date);
}
?3.在接收值的地方進(jìn)行值的接收
calendar.calendarBlock= ^(NSDate*selectDate){
//獲取到傳過(guò)來(lái)的date};
8.如何讓帶有h5標(biāo)簽的內(nèi)容正確展示
NSAttributedString* attrStr = [[NSAttributedStringalloc]initWithData:[self.shiftDutyRecorfModel.contentdataUsingEncoding:NSUnicodeStringEncoding]options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}documentAttributes:nilerror:nil];
[self.handleWorkView.lab_countsetAttributedText:attrStr];
9.UITableView的Group樣式下頂部空白處理
//分組列表頭部空白處理
UIView *view = [[UIView
alloc] initWithFrame:CGRectMake(0, 0, 0, 0.1)];
self.tableView.tableHeaderView
= view;
10.回跳轉(zhuǎn)到某個(gè)任意界面
//判斷前面有沒(méi)有SecondViewController瞬捕,有的話(huà)pop到SecondViewController頁(yè)面鞍历,否則不進(jìn)行跳轉(zhuǎn)
for(UIViewController*vcinself.navigationController.viewControllers)
{if([vcisKindOfClass:[MyListControllerclass]]){
[self.navigationControllerpopToViewController:vcanimated:YES];
return;}
}