拋出異常
NSException* ex = [NSException exceptionWithName:@"name" reason:@"reason" userInfo:nil];
[ex raise];
UILabel文字添加刪除線
NSAttributedString* attrStr =
[[NSAttributedString alloc] initWithString:@"ddddd"
attributes:
@{ NSFontAttributeName : [UIFont systemFontOfSize:20.f],
NSForegroundColorAttributeName : [UIColor redColor],
NSStrikethroughStyleAttributeName : @(NSUnderlineStyleSingle | NSUnderlinePatternSolid),
NSStrikethroughColorAttributeName : [UIColor blueColor] }];
self.label.attributedText = [attrStr copy];
自定義控件的數(shù)據(jù)源設(shè)置
自定義控件應(yīng)該自己定義相關(guān)的數(shù)據(jù)源怕品,
然后其他數(shù)據(jù)源想要使用這個(gè)自定義控件,
就把自定義控件的模型放到數(shù)據(jù)源模型中饥漫,
然后通過(guò)setValue :forKey :來(lái)進(jìn)行對(duì)自定義控件的模型賦值畅买。
NSString
//去掉空格
NSString* _titleLabelOutSpace = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
//NSString ->NSInteger
[_titleLabelOutSpace integerValue];
NSArray
//NSString NSArray 互轉(zhuǎn)
NSArray *arr = [@"a,b,c" componentsSeparatedByString:@","];
- (void)hasString:(NSString*)str1 string2:(NSString*)str2
{
NSString* string1 = str1;
NSString* string2 = str2;
NSRange range = [string1 rangeOfString:string2];
NSUInteger location = range.location;
NSUInteger length = range.length;
NSString* astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%zd,Leight:%zd", location, length]];
NSLog(@"astring:%@", astring);
}
[self hasString:@"1111111" string2:@"2"];
//astring:Location:9223372036854775807, length:0
[self hasString:@"11111112" string2:@"2"];
//astring:Location:8,Leight:1
可以根據(jù)length來(lái)判斷是否有字段
數(shù)組排序
[self.HelpManager.PublishScopeList sortedArrayUsingComparator:^NSComparisonResult(PublishScopeListModel* obj1, PublishScopeListModel* obj2) {
NSComparisonResult result = [obj1.num compare:obj2.num];
return result == NSOrderedAscending; // 降序:NSOrderedAscending====升序:NSOrderedDescending
}];
為UICollectionView添加頭視圖的兩種方式
- 給UICollectionView添加:UICollectionElementKindSectionHeader
- 設(shè)置UICollectionView的:contentInset
- (UICollectionReusableView*)collectionView:(UICollectionView*)collectionView viewForSupplementaryElementOfKind:(NSString*)kind atIndexPath:(NSIndexPath*)indexPath
{
UICollectionReusableView* cell = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"_CollectionReusableViewIndetifier" forIndexPath:indexPath];
return cell;
}
- (CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
{
return CGSizeMake(ScreenWidth, 44);
}
CGFloat _adH = 120.0;
self.collectionView.contentInset = UIEdgeInsetsMake(_adH, 0, 0, 0);
[self.adImgeView setFrame:CGRectMake(0, CGRectGetMinY(self.collectionView.frame) - _adH, CGRectGetWidth(self.collectionView.frame), _adH)];
TableViewCell創(chuàng)建技巧
#import <UIKit/UIKit.h>
static NSString* SEGPictorialDetailADCellIdentifier = @"SEGPictorialDetailADCellIdentifier";
static NSString* SEGHelpViewCellIdentifier = @"_SEGHelpViewCellIdentifier";
static NSString* SEGHelpViewCellDetailTopIdentifier = @"_SEGHelpViewCellDetailTopIdentifier";
static NSString* SEGHelpViewCellDetailDwonIdentifier = @"_SEGHelpViewCellDetailDwonIdentifier";
@interface SEGPictorialDetailADCell : UITableViewCell
+ (instancetype)cellWithTableView:(UITableView*)tableView;
+ (instancetype)cellWithTableView:(UITableView*)tableView reuseIdentifier:(NSString*)reuseIdentifier;
@end
#import "SEGPictorialDetailADCell.h"
@implementation SEGPictorialDetailADCell
+ (instancetype)cellWithTableView:(UITableView*)tableView
{
SEGPictorialDetailADCell* cell = [tableView dequeueReusableCellWithIdentifier:SEGPictorialDetailADCellIdentifier];
if (cell == nil) {
cell = [[SEGPictorialDetailADCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SEGPictorialDetailADCellIdentifier];
}
return cell;
}
+ (instancetype)cellWithTableView:(UITableView*)tableView reuseIdentifier:(NSString*)reuseIdentifier
{
SEGHelpViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
cell = [[SEGHelpViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
self.CellIdentifier = reuseIdentifier;//通過(guò)記錄不同的ID來(lái)布置對(duì)應(yīng)的界面
if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
}
return self;
}
@end
TableHeaderView動(dòng)態(tài)改變高度
[self.tableView beginUpdates];
[self.tableView setTableHeaderView:self.webView];
[self.tableView endUpdates];
去掉多余的空白的cell
self.contactsTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
切割圖片
UIImage* Image = [UIImage imageNamed:@"RedButton"];
Image = [logBackgroundImage stretchableImageWithLeftCapWidth:logBackgroundImage.size.width * 0.5 topCapHeight:logBackgroundImage.size.height * 0.5];
[self.loginBtn setBackgroundImage:Image forState:UIControlStateNormal];
實(shí)現(xiàn)跳轉(zhuǎn)safari漓糙,phone或message
//調(diào)用 電話phone
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]];
//調(diào)用自帶 瀏覽器 safari
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.baidu.com"]];
//調(diào)用 自帶mail
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://sam@icloud.com"]];
//調(diào)用 SMS
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://111111"]];
//跳轉(zhuǎn)到系統(tǒng)設(shè)置相關(guān)界面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=WIFI"]];
程序中獲取軟件的版本號(hào)和app名稱
應(yīng)用程序的名稱和版本號(hào)等信息都保存在mainBundle的infoDictionary字典中,用下面代碼可以取出來(lái)。
NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
NSString* versionNum =[infoDict objectForKey:@"CFBundleVersion"];//版本名稱
NSString*appName =[infoDict objectForKey:@"CFBundleDisplayName"];//app名稱
在有uinavigation,uitoolbar的情況下弧圆,如何添加一個(gè)全屏的 uiview (作為顯示一個(gè)進(jìn)度框击费,任何區(qū)域無(wú)法觸摸而使用)
[[UIApplication sharedApplication].keyWindow addSubview:waitingview];
在項(xiàng)目用代碼打開AppStore
NSString *url = [NSString stringWithFormat:@"http://itunes.apple.com/cn/app/huo-xing-she-xin-dian/id549425594?l=en&mt=8"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
使屏幕一直保持喚醒狀態(tài)拢蛋?(就是不自動(dòng)黑屏)
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
隱藏UINavigationBar
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationControllersetNavigationBarHidden:YESanimated:YES]; //設(shè)置隱藏
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[self.navigationControllersetNavigationBarHidden:NOanimated:YES];//設(shè)置顯示
[super viewWillDisappear:animated];
}
在navigationviewcontroller中,pop到指定的VC
NSArray *viewControllers=[self.navigationController viewControllers];
UIViewController *controller=[viewControllers objectAtIndex:1]; //跳轉(zhuǎn)到第一個(gè)VC
[self.navigationController popToViewController:controller animated:YES];
獲取手機(jī)硬件信息
[[UIDevice currentDevice] systemName];[[UIDevice currentDevice] systemVersion];
[[UIDevice currentDevice] uniqueIdentifier];
[[UIDevice currentDevice] model];
[[UIDevice currentDevice] name];
真機(jī)上結(jié)果:
System Name: iPhone OSSystem
Version: 4.2.1
Unique ID: 9b5ded78d5fa0ac96250f8b4af0e46f40b96ea6d
Model: iPhone
Name: “wwk”的 iPhone
模擬器上結(jié)果:
System Name: iPhone OSSystem
Version: 4.2
Unique ID: 21FFE0FF-429B-5D0B-96D2-EADCA3203260
Model: iPhone Simulator
Name: iPhone Simulator
URL編碼蔫巩、解碼
// 1. URL編碼:ios中http請(qǐng)求遇到漢字的時(shí)候谆棱,需要轉(zhuǎn)化成UTF-8:
NSString* urlString = @"http://www.baidu.com!name=小明";
NSString * encodingString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//結(jié)果:http://www.baidu.com!name=%E5%B0%8F%E6%98%8E
// 2.URL解碼:請(qǐng)求后,返回的數(shù)據(jù)圆仔,如何顯示的是這樣的格式:%3A%2F%2F垃瞧,此時(shí)需要我們進(jìn)行UTF-8解碼,用到的方法是:
NSString *str = [encodingString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
獲取URL中的細(xì)節(jié)
NSURL* url = [NSURL URLWithString:@"http://www.baidu.com/s?tn=baiduhome_pg&bs=NSRUL&f=8&rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=2709"];
NSLog(@"Scheme: %@", [url scheme]);
NSLog(@"Host: %@", [url host]);
NSLog(@"Port: %@", [url port]);
NSLog(@"Path: %@", [url path]);
NSLog(@"Relative path: %@", [url relativePath]);
NSLog(@"Path components as array: %@", [url pathComponents]);
NSLog(@"Parameter string: %@", [url parameterString]);
NSLog(@"Query: %@", [url query]);
NSLog(@"Fragment: %@", [url fragment]);
NSLog(@"User: %@", [url user]);
NSLog(@"Password: %@", [url password]);
/*
2016-05-27 11:38:23.503 FourNews[2358:116708] Scheme: http
2016-05-27 11:38:23.504 FourNews[2358:116708] Host: www.baidu.com
2016-05-27 11:38:23.504 FourNews[2358:116708] Port: (null)
2016-05-27 11:38:23.504 FourNews[2358:116708] Path: /s
2016-05-27 11:38:23.504 FourNews[2358:116708] Relative path: /s
2016-05-27 11:38:23.504 FourNews[2358:116708] Path components as array: (
"/",
s
)
2016-05-27 11:38:23.504 FourNews[2358:116708] Parameter string: (null)
2016-05-27 11:38:23.505 FourNews[2358:116708] Query: tn=baiduhome_pg&bs=NSRUL&f=8&rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=2709
2016-05-27 11:38:23.505 FourNews[2358:116708] Fragment: (null)
2016-05-27 11:38:23.505 FourNews[2358:116708] User: (null)
2016-05-27 11:38:23.505 FourNews[2358:116708] Password: (null)
*/
獲取URL中的參數(shù)
- (NSDictionary*)dictionaryFromQuery:(NSString*)query usingEncoding:(NSStringEncoding)encoding
{
NSCharacterSet* delimiterSet = [NSCharacterSet characterSetWithCharactersInString:@"&;"];
NSMutableDictionary* pairs = [NSMutableDictionary dictionary];
NSScanner* scanner = [[NSScanner alloc] initWithString:query];
while (![scanner isAtEnd]) {
NSString* pairString = nil;
[scanner scanUpToCharactersFromSet:delimiterSet intoString:&pairString];
[scanner scanCharactersFromSet:delimiterSet intoString:NULL];
NSArray* kvPair = [pairString componentsSeparatedByString:@"="];
if (kvPair.count == 2) {
NSString* key = [[kvPair objectAtIndex:0]
stringByReplacingPercentEscapesUsingEncoding:encoding];
NSString* value = [[kvPair objectAtIndex:1]
stringByReplacingPercentEscapesUsingEncoding:encoding];
[pairs setObject:value forKey:key];
}
}
return [NSDictionary dictionaryWithDictionary:pairs];
}
[**ViewController respondsToSelector:]: message sent to deallocated instance崩潰問(wèn)題解決方法
這是因?yàn)檫@個(gè)UIViewController釋放后荧缘,又再次向這個(gè)VC調(diào)用了某些請(qǐng)求導(dǎo)致的皆警,一般出現(xiàn)這種報(bào)錯(cuò)是UIViewController里面有設(shè)置xxx.delegate=self,這些代理有延時(shí)方法截粗,例如等待網(wǎng)絡(luò)返回等
當(dāng)導(dǎo)航欄直接返回時(shí)信姓,UIViewController已經(jīng)被銷毀,延時(shí)方法再調(diào)用此野指針VC绸罗,便會(huì)導(dǎo)致崩潰
解決方法:
1 在dealloc中設(shè)置xx.delegate = nil 意推,這個(gè)是程序沒有使用ARC才可以
2 在viewWillDisappear方法中把所有xxx.delegate=self的具有延時(shí)方法的delegate置nil,我的做法是不過(guò)有沒有延時(shí)方法珊蟀,
UIView有關(guān)的雜技
//設(shè)置View的圓角:
[view.layer setCornerRadius:15.0];