iOS 开发中需要注意的小地方
生活随笔
收集整理的這篇文章主要介紹了
iOS 开发中需要注意的小地方
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
/** 圖片長度截取方法 */
- (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect )rect{CGImageRef sourceImageRef = [image CGImage];CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);UIImage *newImage = [UIImage imageWithCGImage:newImageRef];return newImage;
}SDWebImage清除緩存
#warning 注意: 清除緩存 清除緩存,一般這個清除緩存,在用戶的設(shè)置里面進(jìn)行操作. 如果緩存清除了.雖然減少了內(nèi)存空間.但是,用戶就要再請求一邊,這樣會費(fèi)流量啊.[[SDImageCache sharedImageCache] clearDisk];//清除緩存
CollecttionView
#warning 注意:布局子視圖方法
//優(yōu)勢
// layoutAttributes可以獲取到當(dāng)前cell的信息
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
#warning 注意 層級關(guān)系...要是先添加imv 的話 我的lable 就被檔下去了,文字就不見了_lable.frame = CGRectMake(0, layoutAttributes.frame.size.height - 30, layoutAttributes.frame.size.width, 30);_lable.backgroundColor = [UIColor blackColor];_lable.textColor = [UIColor whiteColor];_lable.textAlignment = NSTextAlignmentCenter;_lable.alpha = 0.33;_imv.frame = CGRectMake(0, 0, layoutAttributes.frame.size.width, layoutAttributes.frame.size.height);}#warning 注意MRC情況下 set的寫法
- (void)setModel:(Model *)model
{if (_model != model) {[_model release];_model = [model retain];//賦值self.lable.text = [NSString stringWithFormat:@"%@",model.height];[self.imv sd_setImageWithURL:[NSURL URLWithString:model.thumbURL] placeholderImage:[UIImage imageNamed:@"placeHoderImage"]];}
}字符串
#warning 這個比較新鮮, 中文轉(zhuǎn)碼(如果網(wǎng)址中有中文,需要中文轉(zhuǎn)碼)
//賽選中文 / 中文轉(zhuǎn)碼NSString *newStr = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:str]];+ (void)musicModelRequest:(MusicModel *)model{NSString *str = model.lyric;
#warning 拆分字符串,放到數(shù)組中NSArray *array = [str componentsSeparatedByString:@"\n"]; //見到/n就把這個字符串放到數(shù)組中//區(qū)分時間與歌詞NSMutableArray *timeMutableArray = [NSMutableArray array]; //接收時間NSMutableArray *lyricMutableArray = [NSMutableArray array]; //接收歌詞for (NSString *string in array) {NSArray *timeAndLyric = [string componentsSeparatedByString:@"]"];//拆分時間與歌詞
[lyricMutableArray addObject:timeAndLyric.lastObject];NSArray *timeArray = [timeAndLyric.firstObject componentsSeparatedByString:@"["];//拆分時間
[timeMutableArray addObject:timeArray.lastObject];}//賦值model.timeArray = [NSArray arrayWithArray:timeMutableArray];model.lyricArray = [NSArray arrayWithArray:lyricMutableArray];}XIB StorryBorad可視化
#warning 注意: View創(chuàng)建方法...
- (void)loadView
{//參數(shù)1要加載的XIB文件 參數(shù)2文件擁有者 參數(shù)3加載選項(xiàng)self.htView = [[NSBundle mainBundle] loadNibNamed:@"HTView" owner:self options:nil].firstObject;self.view = _htView;
}#warning 注意: View改文字改屬性,在這兒
#import "TextView.h"@implementation TextView//改文字,改屬性在這兒
- (void)awakeFromNib
{self.titleLable.text = @"改文字,改屬性在這兒";
}@end#warning 注意:Cell注冊.....
//注冊
// [self.tableView registerClass:[SecondViewController class] forCellReuseIdentifier:cell_id_Second];[self.tableView registerNib:[UINib nibWithNibName:@"SecondTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:cell_id_Second];#warning 注意:使用storyBoard關(guān)聯(lián)的類不再使用init方法創(chuàng)建,直接去storyBorad里面找有當(dāng)前標(biāo)識符的視圖控制器的拖放面板.
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{UIStoryboard *st = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];//1、先去當(dāng)前包里找到故事版
TextViewController *textVC = [st instantiateViewControllerWithIdentifier:@"testViewController"];//2、根據(jù)標(biāo)示符,在故事版中找到當(dāng)前視圖控制器
[self.navigationController pushViewController:textVC animated:YES];}#warning 獲取路徑
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {NSIndexPath *path = [self.tableView indexPathForCell:sender]; //獲取路徑。。。。。 關(guān)鍵呀.******************************************************
DetailViewController *detal = segue.destinationViewController;detal.string = _array[path.row];
}TabBar
#warning 圖片這塊注意:@2x作用,系統(tǒng)自動匹配大小.#warning PUS后 隱藏TabBar
- (void)buttonAction:(UIButton *)sender
{TextViewController *text = [[[TextViewController alloc] init] autorelease];text.hidesBottomBarWhenPushed = YES;PUS頁面的時候隱藏tabBar[self.navigationController pushViewController:text animated:YES];
//有些應(yīng)用點(diǎn)擊的時候隱藏 導(dǎo)航欄 和 標(biāo)簽欄. 是這樣做的
// self.navigationController.navigationBar.hidden = YES;
// self.tabBarController.tabBar.hidden = YES;//在手勢點(diǎn)擊事件里設(shè)置。根據(jù)點(diǎn)擊此時,奇偶數(shù)進(jìn)行判斷就OK了
}#pragma mark 當(dāng)點(diǎn)擊標(biāo)簽欄的時候觸發(fā)此方法
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{if (viewController.tabBarItem.badgeValue != nil) {viewController.tabBarItem.badgeValue = nil;}NSLog(@"%ld",tabBarController.selectedIndex);//輸出選中下標(biāo),做下演示
}BLOCK
//MRC銷毀
- (void)dealloc
{
#warning block 的釋放只能使用Block_release() //系統(tǒng)寫好的釋放方法Block_release(_myBlock); //因?yàn)閎lock不是一個對象類型不能使用relese,是函數(shù).Block_release(_cBlock);//釋放
[_textField release];[super dealloc];NSLog(@"驗(yàn)證dealloc成功");
}- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{#warning //block就是一個回調(diào)函數(shù),相當(dāng)于小弟,什么時候用,什么時候調(diào)。 哈哈//block(函數(shù)) 調(diào)用
_myBlock(_textField.text);#warning 解決循環(huán)引用問題 //記住:Block 實(shí)現(xiàn)部分不能直接使用屬性、實(shí)力變量 和 self 去調(diào)用; 因?yàn)闀粘裳h(huán)引用就是相互持有(不會走dealloc方法), 例如:[self text];這樣調(diào)用, 錯得。循環(huán)引用了.//解決循環(huán)引用問題演示:
//MRC://第一種寫法__block SecondViewController *temp = self;//第二種寫法://__block typeof(self) temp = self;//ARC://__weak SecondViewController *temp = self;//實(shí)現(xiàn)self.cBlock = ^(){[temp text];};//調(diào)用
temp.cBlock();[self.navigationController popViewControllerAnimated:YES];
}#import <UIKit/UIKit.h>
typedef void(^MyBlock) (NSString *); //定義有參,無返回值的匿名函數(shù)傳遞字符串; 這是函數(shù)
typedef void (^Block)(); //做內(nèi)存測試@interface SecondViewController : UIViewController@property(nonatomic,retain)UITextField *textField;//MRC: block的語義設(shè)置是copy 把block從棧區(qū) 拷貝到 堆區(qū) 。使用完之后,在dealoc 里面釋放.(blcok是函數(shù),函數(shù)在棧區(qū))
//ARC:block的語義設(shè)置使用strong 即可
@property(nonatomic,copy)MyBlock myBlock; //參數(shù) 函數(shù)
@property(nonatomic,copy)Block cBlock;@end單例
#warning 單例
//創(chuàng)建靜態(tài)區(qū)對象的原因:希望程序運(yùn)行期間它在程序中一直存在,這樣對外界來說,可以隨時讀取數(shù)據(jù).
//我們創(chuàng)建單例使用加號方法的原因是因?yàn)?在創(chuàng)建之前,無法存在一個實(shí)例對象去調(diào)用動態(tài)方法來創(chuàng)建它本身
/////劣勢:創(chuàng)建多了,會影響性能,內(nèi)存空間減小。程序會卡.因?yàn)槭窃谕A綮o態(tài)區(qū)。生命周期,與程序同步。
/////優(yōu)勢:降低耦合性,降低復(fù)雜性; 可以全局訪問 ;在內(nèi)存中只有一個對象,節(jié)省內(nèi)存空間;避免頻繁的創(chuàng)建銷毀對象,提高性能;避免對共享資源的多重占用.//完整單例手寫吧
//+ (instancetype)sharedDaraHandle
//{
// static DataHandle *dataHandle = nil;
// static dispatch_once_t onceToken;
// dispatch_once(&onceToken, ^{
//
// dataHandle = [[DataHandle alloc] init];
//
// });
//
// return dataHandle;
//}
文本高度自適應(yīng) + 圖片高度自適應(yīng) + Cell注意事項(xiàng)
//heightForRowAtIndexPath 這個方法會比cellForRowAtIndexPath先執(zhí)行,所以我們需要先計(jì)算好cell高度,
#warning 注意:注意:注意:只有cell要在layoutSubViews里面布局,因?yàn)檫@個方法最后走,cell需要重用.所以在這里布局.
#warning 注意:最后走這個方法, 還有 圖片自適應(yīng)及文本自適應(yīng)! 以及l(fā)ayoutSubviews方法里self的寬 和 高 與 初始化里面的不同.
//最后走這個方法
- (void)layoutSubviews
{[super layoutSubviews];
#warning 注意:注意:注意:這里千萬不能這樣賦值 _lable.frame = self.frame 要這樣_lable.frame = self.contentView.frame
#warning 注意:注意:注意:這里千萬不能這樣賦值 _lable.frame = self.frame 要這樣_lable.frame = self.contentView.frame
#warning 注意:注意:注意:這里千萬不能這樣賦值 _lable.frame = self.frame 要這樣_lable.frame = self.contentView.frame//圖片高度自適應(yīng)CGFloat height = self.imv.image.size.height * self.contentView.frame.size.width / self.imv.image.size.width;self.imv.frame = CGRectMake(0, 0, self.contentView.frame.size.width, height);self.imv.backgroundColor = [UIColor grayColor];//lable高度自適應(yīng)
CGFloat lableHeight = [self myHeight:self.lable.text];self.lable.frame = CGRectMake(0, height, self.contentView.frame.size.width, lableHeight);self.lable.backgroundColor = [UIColor orangeColor];self.lable.numberOfLines = 0;self.lable.font = [UIFont systemFontOfSize:17];NSLog(@"--------W:%lf H:%lf",self.frame.size.width,self.frame.size.height);
}- (CGFloat)myHeight:(NSString *)string
{CGRect rect = [string boundingRectWithSize:CGSizeMake(self.frame.size.width, 100000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];return rect.size.height;
}#warning UILable和UIImageView 的用戶交互,默認(rèn)是關(guān)閉的懶加載
#pragma mark 懶加載 重寫get方法 //原來這也是懶加載,實(shí)質(zhì)就是get方法(懶加載——也稱為延遲加載,即在需要的時候才加載(效率低,占用內(nèi)存小)。)
- (void)addAllViews{[self addSubview:self.nameLabel]; //懶加載(用了點(diǎn)語法,才能調(diào)用get方法)[self addSubview:self.nameTextField]; //懶加載
}
#pragma mark 懶加載 重寫get方法
- (UILabel *)nameLabel{if (!_nameLabel) {
#warning 注意,創(chuàng)建的時候一定要用self. 點(diǎn)語法創(chuàng)建, 不要_nameLable這樣取創(chuàng)建self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];_nameLabel.text = @"懶加載";}return _nameLabel;
}#pragma mark 布局子視圖
//布局子視圖(從新布局view. 此方法,自動觸發(fā),程序最后走這個方法)
- (void)layoutSubviews{
//[UIApplication sharedApplication].statusBarOrientation//statusBarOrientation狀態(tài)欄方向(應(yīng)用程序)if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight) {[super layoutSubviews];_nameTextField.frame = CGRectMake(150, 20, 400, 50);_nameTextField.backgroundColor = [UIColor redColor];//如果是橫屏,XXXX
}else{[super layoutSubviews];//如果是豎屏,XXXX_nameTextField.frame = CGRectMake(0, 120, 150, 30);_nameTextField.backgroundColor = [UIColor whiteColor];}//(這里是根據(jù)應(yīng)用程序的狀態(tài)欄方向,來從新布局子時視圖)
}//此方法,繪圖的時候,用
- (void)drawRect:(CGRect)rect {// Drawing code 繪圖時候用
}
從寫Model的Get方法 賦值時, 注意: 在if {} 花括號外邊進(jìn)行.
?
轉(zhuǎn)載于:https://www.cnblogs.com/HaiTeng/p/5324130.html
總結(jié)
以上是生活随笔為你收集整理的iOS 开发中需要注意的小地方的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JMeter基础之—录制脚本
- 下一篇: [Python]网络爬虫(十):一个爬虫