一、調(diào)節(jié)UINavigationBar的leftBarButtonItem離左邊的距離
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back-button-whiteArrow.png"] style:UIBarButtonItemStylePlain target:self action:@selector(logoutBarBtnPressed:)];
UIBarButtonItem *fixedBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedBarButtonItem.width = -15;
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:fixedBarButtonItem, buttonItem, nil];
二、RestKit 保存數(shù)據(jù)到core data
通過(guò)RestKit將數(shù)據(jù)保存到core data中,entity為
@interface Article : NSManagedObject
@property (nonatomic, retain) NSNumber* articleID;
@property (nonatomic, retain) NSString* title;
@property (nonatomic, retain) NSString* body;
@property (nonatomic, retain) NSDate* publicationDate;
@end
@implementation Article // We use @dynamic for the properties in Core Data
@dynamic articleID;
@dynamic title;
@dynamic body;
@dynamic publicationDate;
@end
設(shè)置object mapping
RKEntityMapping* articleMapping = [RKEntityMapping mappingForEntityForName:@"Article"
inManagedObjectStore:managedObjectStore];
[articleMapping addAttributeMappingsFromDictionary:@{
@"id": @"articleID",
@"title": @"title",
@"body": @"body",
@"publication_date": @"publicationDate"
}];
articleMapping.identificationAttributes = @[ @"articleID" ];
其中的identificationAttributes 設(shè)置的數(shù)組中的值,就是用來(lái)判斷返回的數(shù)據(jù)是更新還是new。
三、RestKit 添加relationship
Author Entity
@interface Author : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *email;
@end
Article Entity
@interface Article : NSObject
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *body;
@property (nonatomic) Author *author;
@property (nonatomic) NSDate *publicationDate;
@end
添加好關(guān)系friendship
// Create our new Author mapping
RKObjectMapping* authorMapping = [RKObjectMapping mappingForClass:[Author class] ];
// NOTE: When your source and destination key paths are symmetrical, you can use addAttributesFromArray: as a shortcut instead of addAttributesFromDictionary:
[authorMapping addAttributeMappingsFromArray:@[ @"name", @"email" ]];
// Now configure the Article mapping
RKObjectMapping* articleMapping = [RKObjectMapping mappingForClass:[Article class] ];
[articleMapping addAttributeMappingsFromDictionary:@{
@"title": @"title",
@"body": @"body",
@"publication_date": @"publicationDate"
}];
// Define the relationship mapping
[articleMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"author"
toKeyPath:@"author"
withMapping:authorMapping]];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:articleMapping
method:RKRequestMethodAny
pathPattern:nil keyPath:@"articles"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
在Json轉(zhuǎn)Model的時(shí)候,使用JTObjectMapping方法很類似。減少很多的工作量。
四、xcode找不到設(shè)備
原因:Deployment Target版本比真機(jī)版本高,導(dǎo)致找不到。將Deployment Target設(shè)置成與真機(jī)的系統(tǒng)版本一致。
五、autolayout 自動(dòng)布局
autoLayout 需要在- (void)viewDidLoad
方法執(zhí)行完后生效,所以需要在- (void)viewDidAppear:(BOOL)animated
方法中再進(jìn)行frame的獲取,此時(shí)才能取到正確的frame。
六、Navigation backBarButtonItem 設(shè)置
根據(jù)蘋果官方指出:backbarbuttonItem不能定義customview,所以,只能貼圖或者,讓leftBarButtonItem變成自定義返回按鈕,自己寫個(gè)方法進(jìn)行[self.navigationController pop當(dāng)前Item
之前大家是否疑惑為什么設(shè)置了類似這樣的代碼
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:@"返回"
style:UIBarButtonItemStylePlain
target:self
action:nil];
self.navigationItem.backBarButtonItem = backButton;
界面上backButton并沒(méi)出現(xiàn)“返回”的字樣.
其實(shí)是被leftBarButtonItem和rightBarButtonItem的設(shè)置方法所迷惑了leftBarButtonItem和rightBarButtonItem設(shè)置的是本級(jí)頁(yè)面上的BarButtonItem,而backBarButtonItem設(shè)置的是下一級(jí)頁(yè)面上的BarButtonItem.比如:兩個(gè)ViewController,主A和子B,我們想在A上顯示“刷新”的右BarButton,B上的BackButton顯示為“撤退”就應(yīng)該在A的viewDidLoad類似方法中寫:
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc]
initWithTitle:@"刷新"
style:UIBarButtonItemStylePlain
target:self
action:nil];
self.navigationItem.rightBarButtonItem = refreshButton;
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
initWithTitle:@"撤退"
style:UIBarButtonItemStylePlain
target:self
action:nil];
self.navigationItem.backBarButtonItem = cancelButton;
而B(niǎo)不需要做任何處理然后ApushB就可以了.
七、AFNetworking 使用ssl
sharedClient.securityPolicy.allowInvalidCertificates = YES;
八、NSJSONSerialization 使用數(shù)據(jù)類型要求
進(jìn)行JSON轉(zhuǎn)化的時(shí)候,需要滿足一下的要求。
An object that may be converted to JSON must have the following properties:
The top level object is an NSArray or NSDictionary.
All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.
All dictionary keys are instances of NSString.
Numbers are not NaN or infinity.
也就是說(shuō):nil,基礎(chǔ)數(shù)據(jù)不能轉(zhuǎn)化為JSON。
九、NSArray進(jìn)行不定參數(shù)處理
+ (NSArray *)arrayWithObjectsExceptionNil:(id)firstObj, ...
{
NSMutableArray *tempMArray = [[NSMutableArray alloc] initWithCapacity:5];
id eachObject = nil;
va_list argumentList;
if ( firstObj ) {
[tempMArray addObject:firstObj];
va_start(argumentList, firstObj);
while ( (eachObject = va_arg(argumentList, id))){
if ( nil != eachObject ){
[tempMArray addObject:eachObject];
}
}
va_end(argumentList);
}
return nil;
}
十、獲取version
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
// app名稱
NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
// app版本
NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
// app build版本
NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];
注意:appid,在申請(qǐng)?zhí)峤坏臅r(shí)候,在itunesconnect的這個(gè)里面生成了,審核通過(guò)了也不會(huì)改變。
十一、no input file 錯(cuò)誤
如果在編譯的時(shí)候找不到文件,需要先在Build Phases中的Compile Sources 將不存在的文件刪除,然后再將找不到的文件添加到project中。
十二、cg,cf,ca,ui等開(kāi)頭類
你還可以看到其他名字打頭的一些類,比如CF、CA、CG、UI等等,比如CFStringTokenizer 這是個(gè)分詞的東東CALayer 這表示Core Animation的層CGPoint 這表示一個(gè)點(diǎn)UIImage 這表示iPhone里面的圖片
CF說(shuō)的是Core Foundation,CA說(shuō)的是Core Animation,CG說(shuō)的是Core Graphics,UI說(shuō)的是iPhone的User Interface
十三、file's owner 含義
file's owner 就是xib對(duì)應(yīng)的類,如view對(duì)應(yīng)的xib文件的file's owner對(duì)應(yīng)的類就是viewcontroller的類。file’s owner 是view和viewcontroller之間的對(duì)應(yīng)關(guān)系的橋梁。(即,一個(gè)視圖,如何知道自己的界面的操作應(yīng)該由誰(shuí)來(lái)響應(yīng))
十四、iOS coin 不透明
一般iOS app coin應(yīng)該是不透明的,并且不可以在app中多次使用app coin。
十五、判斷自定義類是否重復(fù)
自定義類庫(kù)中,需要重寫NSObject的兩個(gè)固定方法來(lái)判斷類是否重復(fù):
- (BOOL)isEqual:(id)anObject;
- (NSUInteger)hash;
十六、IOS常用宏
// Macro wrapper for NSLog only if debug mode has been enabled
#ifdef DEBUG
#define DLog(fmt,...) NSLog(fmt, ##__VA_ARGS__);
#else
// If debug mode hasn't been enabled, don't do anything when the macro is called
#define DLog(...)
#endif
#define MR_ENABLE_ACTIVE_RECORD_LOGGING 0
#define IS_OS_6_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
#define IS_OS_7_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
#define OBJISNULL(o) (o == nil || [o isKindOfClass:[NSNull class]] || ([o isKindOfClass:[NSString class]] && [o length] == 0))
#define APP ((AppDelegate*)[[UIApplication sharedApplication] delegate])
#define UserDefaults [NSUserDefaults standardUserDefaults]
#define SharedApplication [UIApplication sharedApplication]
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
#define RGB(r, g, b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]
#define RGBA(r, g, b, a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
十七、判斷是否是4寸屏
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
十八、NSString格式限定符
說(shuō)明符 描述
%@ Objective-c 對(duì)象
%zd NSInteger
%lx CFIndex
%tu NSUInteger
%i int
%u unsigned int
%hi short
%hu unsigned short
%% %符號(hào)
十九、聲明變量 在ARC下自動(dòng)初始化為nil
NSString *s;
在非ARC的情況下, s指向任意地址,可能是系統(tǒng)地址,導(dǎo)致崩潰。
在ARC的情況下,s已經(jīng)自動(dòng)初始化為nil。
二十、向NSArray,NSDictionary等容器添加元素需判nil
[_paths addObject:[_path copy]];
如果這個(gè)_path為nil,那么就會(huì)出現(xiàn)一個(gè)crash。因?yàn)樵谌萜髦胁荒艽娣舗il,可以用[NSNull null]來(lái)保存.
推薦 pod 'XTSafeCollection', '~> 1.0.4'
第三方庫(kù),對(duì)數(shù)組的越界,賦值nil,都有保護(hù)作用。
二十一、ceil命令 floor命令
Math中一個(gè)算法命令。函數(shù)名: ceil用 法: double ceil(double x);功 能: 返回大于或者等于指定表達(dá)式的最小整數(shù)頭文件:math.h
float f = 1.2222;
NSLog(@"f is %f.", ceil(f));
打印: f is 2.000000.
函數(shù)名: floor功 能: 返回小于或者等于指定表達(dá)式的最大整數(shù)用 法: double floor(double x);頭文件:math.h
float f = 1.2222;NSLog(@"f is %f.", floor(f));
打?。?f is 1.000000.
二十二、Xcode中對(duì)某個(gè)類進(jìn)行非ARC的設(shè)置
在Xcode點(diǎn)擊工程,在工程中選擇“TARGETS”你的工程,在Build Phases中選擇“Compile Sources”,找到你需要設(shè)置非ARC的類,在這個(gè)類的右邊有一個(gè)“Compiler Flags”,在這個(gè)里面設(shè)置“-fno-objc-arc”。那么這個(gè)類就是非ARC進(jìn)行編譯了。
二十三、storyboard上不能進(jìn)行scrollview滾動(dòng)
storyboard上不能進(jìn)行scrollview滾動(dòng)的原因是: autolayout引起的
二十四、延長(zhǎng)APP的啟動(dòng)時(shí)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[NSThread sleepForTimeInterval:3.0f];
return YES;
}
二十五、xcode調(diào)試的時(shí)候不運(yùn)行watchdog
在利用Xcode進(jìn)行調(diào)試時(shí),watchdog不會(huì)運(yùn)行,所在設(shè)備中測(cè)試程序啟動(dòng)性能時(shí),不要將設(shè)備連接到Xcode。
二十六、語(yǔ)言國(guó)際化 NSLocalizedString
如果你使用的是Localizable.strings,那么你在程序中可以這樣獲取字符串:NSLocalizedString(@"mykey", nil)
如果你使用的是自定義名字的.strings,比如MyApp.strings,那么你在程序中可以這樣獲取字符串:
NSLocalizedStringFromTable (@"mykey",@"MyApp", nil)
這樣即可獲取到"myvalue"這個(gè)字符串,可以是任何語(yǔ)言。
二十七、UIAppearance 使用
使用UIAppearance進(jìn)行外觀的自定義。
[+ appearance]
修改整個(gè)程序中某個(gè)class的外觀
[[UINavigationBar appearance] setTintColor:myColor];
[+ appearanceWhenContainedIn:]
當(dāng)某個(gè)class被包含在另外一個(gè)class內(nèi)時(shí),才修改外觀。
[[UILabel appearanceWhenContainedIn:[cusSearchBar class], nil] setTextColor:[UIColor redColor]];
二十八、將NSString轉(zhuǎn)換成UTF8編碼的NSString
在使用網(wǎng)絡(luò)地址時(shí),一般要先將url進(jìn)行encode成UTF8格式的編碼,否則在使用時(shí)可能報(bào)告網(wǎng)址不存在的錯(cuò)誤,這時(shí)就需要進(jìn)行轉(zhuǎn)換下面就是轉(zhuǎn)換函數(shù):
NSString *urlString= [NSString stringWithFormat:@"http://www.baidu.com"];
NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, (CFStringRef)urlString, NULL, NULL, kCFStringEncodingUTF8 ));
NSURL *url = [NSURL URLWithString:encodedString];
或者使用下面的方法:
NSString *utf8Str = @"Testing";NSString *unicodeStr = [NSString stringWithCString:[utf8Str UTF8String] encoding:NSUnicodeStringEncoding];
有時(shí)候獲取的url中的中文等字符是亂碼,網(wǎng)頁(yè)內(nèi)容是亂碼,需要進(jìn)行一下轉(zhuǎn)碼才能正確識(shí)別NSString,可以用下面的方法:
//解決亂碼問(wèn)題()
NSString *transString = [NSString stringWithString:[string stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
二十九、NSDateFormatter設(shè)定日期格式 AM
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setAMSymbol:@"AM"];
[dateFormatter setPMSymbol:@"PM"];
[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mmaaa"];
NSDate *date = [NSDate date];
NSString *s = [dateFormatter stringFromDate:date];
顯示效果為:10/05/2010 03:49PM
三十、判斷NSString為純數(shù)字
//判斷是否為整形:
- (BOOL)isPureInt:(NSString*)string{
NSScanner* scan = [NSScanner scannerWithString:string];
int val;
return[scan scanInt:&val] && [scan isAtEnd];
}
//判斷是否為浮點(diǎn)形:
- (BOOL)isPureFloat:(NSString*)string{
NSScanner* scan = [NSScanner scannerWithString:string];
float val;
return[scan scanFloat:&val] && [scan isAtEnd];
}
if( ![self isPureInt:insertValue.text] || ![self isPureFloat:insertValue.text])
{
resultLabel.textColor = [UIColor redColor];
resultLabel.text = @"警告:含非法字符,請(qǐng)輸入純數(shù)字!";
return;
}
三十一、image的正確使用
iOS中從程序bundle中加載UIImage一般有兩種方法。第一種比較常見(jiàn):imageNamed
第二種方法很少使用:imageWithContentsOfFile
為什么有兩種方法完成同樣的事情呢?imageNamed的優(yōu)點(diǎn)在于可以緩存已經(jīng)加載的圖片。蘋果的文檔中有如下說(shuō)法:This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.
這種方法會(huì)在系統(tǒng)緩存中根據(jù)指定的名字尋找圖片,如果找到了就返回。如果沒(méi)有在緩存中找到圖片,該方法會(huì)從指定的文件中加載圖片數(shù)據(jù),并將其緩存起來(lái),然后再把結(jié)果返回。
而imageWithContentsOfFile
方法只是簡(jiǎn)單的加載圖片,并不會(huì)將圖片緩存起來(lái)。這兩個(gè)方法的使用方法如下:
UIImage *img = [UIImage imageNamed:@"myImage"]; // caching // or UIImage *img = [UIImage imageWithContentsOfFile:@"myImage"]; // no caching
那么該如何選擇呢?
如果加載一張很大的圖片,并且只使用一次,那么就不需要緩存這個(gè)圖片。這種情況imageWithContentsOfFile比較合適——系統(tǒng)不會(huì)浪費(fèi)內(nèi)存來(lái)緩存圖片。
然而,如果在程序中經(jīng)常需要重用的圖片,那么最好是選擇imageNamed方法。這種方法可以節(jié)省出每次都從磁盤加載圖片的時(shí)間。
三十二、將圖片中間部分放大
根據(jù)圖片上下左右4邊的像素進(jìn)行自動(dòng)擴(kuò)充。
UIImage *image = [UIImage imageNamed:@"png-0016"];
UIImage *newImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(50, 50, 50, 50) resizingMode:UIImageResizingModeStretch];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 200, 400)];
imageView.image = newImage;
imageView.contentMode = UIViewContentModeScaleAspectFill;
使用方法- (UIImage )resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode NS_AVAILABLE_IOS(6_0); // the interior is resized according to the resizingMode
進(jìn)行對(duì)圖片的拉伸,可以使用UIImageResizingModeTile和UIImageResizingModeStretch兩種拉伸方式。*注意: **此方法返回一個(gè)新的UIImage,需要使用這個(gè)新的image。
注:UIEdgeInsets設(shè)置的值不要上下或左右交叉,不然會(huì)出現(xiàn)中間為空白的情況。
在Xcode5中也可以使用新特性 Slicing,直接對(duì)圖片進(jìn)行設(shè)置,不需要在代碼中設(shè)置了。
三十三、UIImage和NSData的轉(zhuǎn)換
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
UIImage *aimage = [UIImage imageWithData:imageData];
//UIImage 轉(zhuǎn)化為 NSData
NSData *imageData = UIImagePNGRepresentation(aimage);
三十四、NSDictionary和NSData轉(zhuǎn)換
// NSDictionary -> NSData:
NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDictionary];
// NSData -> NSDictionary:
NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:myData];
三十五、NSNumberFormatter 價(jià)格采用貨幣模式
如果顯示的數(shù)值為價(jià)格,則用貨幣模式
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
self.introView.guaranteedDataLabel.text = [formatter stringFromNumber:self.quoteEntity.guranteedInfo.guaranteedPrice];
三十六、畫虛線的方法
CGFloat lengths[] = {5, 5};
CGContextRef content = UIGraphicsGetCurrentContext();
CGContextBeginPath(content);
CGContextSetLineWidth(content, LINE_WIDTH);
CGContextSetStrokeColorWithColor(content, [UIColor blackColor].CGColor);
CGContextSetLineDash(content, 0, lengths, 2);
CGContextMoveToPoint(content, 0, rect.size.height - LINE_WIDTH);
CGContextAddLineToPoint(content, rect.size.width, rect.size.height - LINE_WIDTH);
CGContextStrokePath(content);
CGContextClosePath(content);
三十七、設(shè)備轉(zhuǎn)屏
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeLeft;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft;
}
在storyboard中設(shè)置只支持豎屏,可以在單個(gè)UIViewController中加入這3個(gè)方法,使得這個(gè)UIViewController只支持左橫屏。
三十八、UITextField 彈出UIDatePicker
設(shè)置UITextField的inputView可以不彈出鍵盤,而彈出UIDatePicker。
首先需要在View加載結(jié)束的時(shí)候指定文本的InputView
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
self.txtDate.inputView = datePicker;
然后需要指定當(dāng)UIDatePicker變動(dòng)的時(shí)候的事件是什么. 此處就是為了給文本框賦值.
- (IBAction)dateChanged:(id)sender
{
UIDatePicker *picker = (UIDatePicker *)sender;
self.txtDate.text = [NSString stringWithFormat:@"%@", picker.date];
}
然后當(dāng)文本框編輯結(jié)束時(shí), 需要讓UIDatePicker消失.
- (IBAction)doneEditing:(id)sender
{
[self.txtDate resignFirstResponder];
}
然后把文本框在IB中, 指向定義好的txtDate就行了~
三十九、將Status Bar字體設(shè)置為白色
在Info.plist中設(shè)置UIViewControllerBasedStatusBarAppearance
為NO
在需要改變狀態(tài)欄顏色的ViewController中在ViewDidLoad方法中增加:
UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
如果需要在全部View中都變色,可以寫在父類的相關(guān)方法中。
四十、UILongPressGestureRecognizer執(zhí)行2次的問(wèn)題
//會(huì)調(diào)用2次,開(kāi)始時(shí)和結(jié)束時(shí)
- (void)hello:(UILongPressGestureRecognizer *)longPress
{
if (longPress.state == UIGestureRecognizerStateEnded)//需要添加一個(gè)判斷
{
NSLog(@"long");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"hello"
message:@"Long Press"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
}
}
四十一、View中事件一次響應(yīng)一個(gè)
self.scrollView.exclusiveTouch = YES;
設(shè)置exclusiveTouch這個(gè)屬性為YES。
四十二、UIScrollView中立即響應(yīng)操作
delaysContentTouches 屬性是UIScrollView中立即響應(yīng)Touch事件。默認(rèn)是YES,如果想點(diǎn)擊后馬上有反應(yīng),則將該值設(shè)置為NO。
四十三、UITableView在Cell上添加自定義view
如果在UITableView上添加一個(gè)自定義的UIView,需要注意在view中的顏色會(huì)因?yàn)镃ell被選中的點(diǎn)擊色,而引起view的顏色變化,并且不可逆。
四十四、NSNotificationCenter 注銷
當(dāng) NSNotificationCenter 注冊(cè)一個(gè)通知后
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullableNSString *)aName object:(nullableid)anObject;
在class的dealloc中,一定要使用
- (void)removeObserver:(id)observer name:(nullable NSString *)aName object:(nullable id)anObject;
進(jìn)行注銷。
不能用 - (void)removeObserver:(id)observer;
進(jìn)行通知的注銷。
注意: 如果不注銷,將導(dǎo)致class不會(huì)被釋放。
四十五、H5開(kāi)發(fā)的弊端
H5開(kāi)發(fā)的弊端: 占用內(nèi)容太多。在打開(kāi)H5的時(shí)候,會(huì)占用大量的內(nèi)存,在項(xiàng)目中看到,一般會(huì)達(dá)到一個(gè)頁(yè)面50M的內(nèi)存。
四十六、interactivepopgesturerecognizer 使用
設(shè)置left bar button后,會(huì)導(dǎo)致右滑返回的效果失效,查看完美的設(shè)置方案。
同時(shí)為了獲取到右滑返回的事件,可以執(zhí)行
[self.navigationController.interactivePopGestureRecognizer addTarget:self action:@selector(back)];
在ViewController中viewDidAppare中添加,在viewWillDisappear中remove。
四十六、masonry 中使用兩個(gè)UIView之間設(shè)置layout
[self.bottomOpenStoreBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(promptImageView.mas_bottom).with.offset(30);
make.height.mas_equalTo(44);
make.width.mas_equalTo(SCREEN_WIDTH - 30);
make.centerX.mas_equalTo(self.promptScrollView);
make.bottom.mas_equalTo(self.promptScrollView.mas_bottom).with.offset(-30);
}];
設(shè)置self.bottomOpenStoreBtn的頂部與promptImageView的頂部距離30ptmake.bottom.mas_equalTo(self.promptScrollView.mas_bottom).with.offset(-30);
其中的 self.promptScrollView.mas_bottom 必須使用mas_bottom,不能使用bottom.
make.top 中的top為- (MASConstraint*)top
而self.promptScrollView.bottom 中的bottom為
- (float) bottom
{ return CGRectGetMaxY (self.frame);
}
即一個(gè)是layout屬性,另一個(gè)為frame的屬性。