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];
复制代码
fixedButton.width
配置离左边的距离。
经过RestKit将数据保存到core data中,entity为html
@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
复制代码
设置object mappingios
RKEntityMapping* articleMapping = [RKEntityMapping mappingForEntityForName:@"Article"
inManagedObjectStore:managedObjectStore];
[articleMapping addAttributeMappingsFromDictionary:@{
@"id": @"articleID",
@"title": @"title",
@"body": @"body",
@"publication_date": @"publicationDate"
}];
articleMapping.identificationAttributes = @[ @"articleID" ];
复制代码
其中的identificationAttributes 设置的数组中的值,就是用来判断返回的数据是更新仍是new。算法
Author Entity数组
@interface Author : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *email;
@end
复制代码
Article Entityxcode
@interface Article : NSObject
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *body;
@property (nonatomic) Author *author;
@property (nonatomic) NSDate *publicationDate;
@end
复制代码
添加好关系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转Model的时候,使用JTObjectMapping方法很相似。减小不少的工做量。bash
缘由: Deployment Target版本比真机版本高,致使找不到。将Deployment Target设置成与真机的系统版本一致。网络
autoLayout 须要在- (void)viewDidLoad
方法执行完后生效,因此须要在- (void)viewDidAppear:(BOOL)animated
方法中再进行frame的获取,此时才能取到正确的frame。app
**根据苹果官方指出:backbarbuttonItem不能定义customview,因此,只能贴图或者,让leftBarButtonItem变成自定义返回按钮,本身写个方法进行[self.navigationController pop当前Item **ide
以前你们是否疑惑为何设置了相似这样的代码
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:@"返回"
style:UIBarButtonItemStylePlain
target:self
action:nil];
self.navigationItem.backBarButtonItem = backButton;
复制代码
界面上backButton并没出现“返回”的字样.
实际上是被leftBarButtonItem和rightBarButtonItem的设置方法所迷惑了leftBarButtonItem和rightBarButtonItem设置的是本级页面上的BarButtonItem,而backBarButtonItem设置的是下一级页面上的BarButtonItem. 好比:两个ViewController,主A和子B,咱们想在A上显示“刷新”的右BarButton,B上的BackButton显示为“撤退”就应该在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不须要作任何处理而后ApushB就能够了.
sharedClient.securityPolicy.allowInvalidCertificates = YES;
复制代码
进行JSON转化的时候,须要知足一下的要求。
An object that may be converted to JSON must have the following properties:
也就是说:nil,基础数据不能转化为JSON。
+ (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;
}
复制代码
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,在申请提交的时候,在itunesconnect的这个里面生成了,审核经过了也不会改变。 **
若是在编译的时候找不到文件,须要先在Build Phases中的Compile Sources 将不存在的文件删除,而后再将找不到的文件添加到project中。
你还能够看到其余名字打头的一些类,好比CF、CA、CG、UI等等,好比 CFStringTokenizer 这是个分词的东东 CALayer 这表示Core Animation的层 CGPoint 这表示一个点 UIImage 这表示iPhone里面的图片
CF说的是Core Foundation,CA说的是Core Animation,CG说的是Core Graphics,UI说的是iPhone的User Interface
file's owner 就是xib对应的类,如view对应的xib文件的file's owner对应的类就是viewcontroller的类。 file’s owner 是view和viewcontroller之间的对应关系的桥梁。(即,一个视图,如何知道本身的界面的操做应该由谁来响应)
通常iOS app coin应该是不透明的,而且不能够在app中屡次使用app coin。
自定义类库中,须要重写NSObject的两个固定方法来判断类是否重复:
- (BOOL)isEqual:(id)anObject;
- (NSUInteger)hash;
复制代码
// 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]
复制代码
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
复制代码
说明符 | 描述 |
---|---|
%@ | Objective-c 对象 |
%zd | NSInteger |
%lx | CFIndex |
%tu | NSUInteger |
%i | int |
%u | unsigned int |
%hi | short |
%hu | unsigned short |
%% | %符号 |
NSString *s;
在非ARC 的状况下, s指向任意地址,多是系统地址,致使崩溃。
在ARC 的状况下,s已经自动初始化为nil。
[_paths addObject:[_path copy]];
若是这个_path为nil,那么就会出现一个crash。由于在容器中不能存放nil,能够用[NSNull null]来保存.
推荐 pod 'XTSafeCollection', '~> 1.0.4'
第三方库,对数组的越界,赋值nil,都有保护做用。
Math中一个算法命令。 函数名: ceil用 法: double ceil(double x);功 能: 返回大于或者等于指定表达式的最小整数头文件:math.h
float f = 1.2222;
NSLog(@"f is %f.", ceil(f));
复制代码
打印: f is 2.000000.
函数名: floor功 能: 返回小于或者等于指定表达式的最大整数用 法: double floor(double x);头文件:math.h
float f = 1.2222;
NSLog(@"f is %f.", floor(f));
复制代码
打印: f is 1.000000.
在Xcode点击工程,在工程中选择“TARGETS”你的工程,在Build Phases中选择“Compile Sources”,找到你须要设置非ARC的类,在这个类的右边有一个“Compiler Flags”,在这个里面设置“-fno-objc-arc ”。 那么这个类就是非ARC进行编译了。
storyboard上不能进行scrollview滚动的缘由是: autolayout引发的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[NSThread sleepForTimeInterval:3.0f];
return YES;
}
复制代码
在利用Xcode进行调试时,watchdog不会运行,所在设备中测试程序启动性能时,不要将设备链接到Xcode。
若是你使用的是Localizable.strings,那么你在程序中能够这样获取字符串: NSLocalizedString(@"mykey", nil)
若是你使用的是自定义名字的.strings,好比MyApp.strings,那么你在程序中能够这样获取字符串: NSLocalizedStringFromTable (@"mykey",@"MyApp", nil)
这样便可获取到"myvalue"这个字符串,能够是任何语言。
使用UIAppearance进行外观的自定义。
[+ appearance]
修改整个程序中某个class的外观
[[UINavigationBar appearance] setTintColor:myColor];
复制代码
[+ appearanceWhenContainedIn:]
当某个class被包含在另一个class内时,才修改外观。
[[UILabel appearanceWhenContainedIn:[cusSearchBar class], nil] setTextColor:[UIColor redColor]];
复制代码
在使用网络地址时,通常要先将url进行encode成UTF8格式的编码,不然在使用时可能报告网址不存在的错误,这时就须要进行转换 下面就是转换函数:
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];
复制代码
有时候获取的url中的中文等字符是乱码,网页内容是乱码,须要进行一下转码才能正确识别NSString,能够用下面的方法:
//解决乱码问题()
NSString *transString = [NSString stringWithString:[string stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
复制代码
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
//判断是否为整形:
- (BOOL)isPureInt:(NSString*)string{
NSScanner* scan = [NSScanner scannerWithString:string];
int val;
return[scan scanInt:&val] && [scan isAtEnd];
}
//判断是否为浮点形:
- (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 = @"警告:含非法字符,请输入纯数字!";
return;
}
复制代码
iOS中从程序bundle中加载UIImage通常有两种方法。 第一种比较常见:imageNamed
第二种方法不多使用:imageWithContentsOfFile
为何有两种方法完成一样的事情呢?imageNamed的优势在于能够缓存已经加载的图片。苹果的文档中有以下说法: 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. 这种方法会在系统缓存中根据指定的名字寻找图片,若是找到了就返回。若是没有在缓存中找到图片,该方法会从指定的文件中加载图片数据,并将其缓存起来,而后再把结果返回。 而imageWithContentsOfFile
方法只是简单的加载图片,并不会将图片缓存起来。这两个方法的使用方法以下:
UIImage *img = [UIImage imageNamed:@"myImage"]; // caching
// or
UIImage *img = [UIImage imageWithContentsOfFile:@"myImage"]; // no caching
复制代码
那么该如何选择呢? 若是加载一张很大的图片,而且只使用一次,那么就不须要缓存这个图片。这种状况imageWithContentsOfFile比较合适——系统不会浪费内存来缓存图片。
然而,若是在程序中常常须要重用的图片,那么最好是选择imageNamed方法。这种方法能够节省出每次都从磁盘加载图片的时间。
根据图片上下左右4边的像素进行自动扩充。
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
进行对图片的拉伸,可使用UIImageResizingModeTile和UIImageResizingModeStretch两种拉伸方式。 **注意: **此方法返回一个新的UIImage,须要使用这个新的image。
注:UIEdgeInsets设置的值不要上下或左右交叉,否则会出现中间为空白的状况。
在Xcode5中也可使用新特性 Slicing,直接对图片进行设置,不须要在代码中设置了。
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
UIImage *aimage = [UIImage imageWithData:imageData];
//UIImage 转化为 NSData
NSData *imageData = UIImagePNGRepresentation(aimage);
复制代码
// NSDictionary -> NSData:
NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDictionary];
// NSData -> NSDictionary:
NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:myData];
复制代码
若是显示的数值为价格,则用货币模式
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);
复制代码
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeLeft;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft;
}
复制代码
在storyboard中设置只支持竖屏,能够在单个UIViewController中加入这3个方法,使得这个UIViewController只支持左横屏。
设置UITextField的inputView能够不弹出键盘,而弹出UIDatePicker。
首先须要在View加载结束的时候指定文本的InputView
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
self.txtDate.inputView = datePicker;
复制代码
而后须要指定当UIDatePicker变更的时候的事件是什么. 此处就是为了给文本框赋值.
- (IBAction)dateChanged:(id)sender
{
UIDatePicker *picker = (UIDatePicker *)sender;
self.txtDate.text = [NSString stringWithFormat:@"%@", picker.date];
}
复制代码
而后当文本框编辑结束时, 须要让UIDatePicker消失.
- (IBAction)doneEditing:(id)sender
{
[self.txtDate resignFirstResponder];
}
复制代码
而后把文本框在IB中, 指向定义好的txtDate就好了~
在Info.plist中设置UIViewControllerBasedStatusBarAppearance
为NO
在须要改变状态栏颜色的ViewController中在ViewDidLoad方法中增长:
UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
复制代码
若是须要在所有View中都变色,能够写在父类的相关方法中。
//会调用2次,开始时和结束时
- (void)hello:(UILongPressGestureRecognizer *)longPress
{
if (longPress.state == UIGestureRecognizerStateEnded)//须要添加一个判断
{
NSLog(@"long");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"hello"
message:@"Long Press"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
}
}
复制代码
self.scrollView.exclusiveTouch = YES;
复制代码
设置exclusiveTouch这个属性为YES。
delaysContentTouches 属性是UIScrollView中当即响应Touch事件。默认是YES,若是想点击后立刻有反应,则将该值设置为NO。
若是在UITableView上添加一个自定义的UIView,须要注意在view中的颜色会由于Cell被选中的点击色,而引发view的颜色变化,而且不可逆。
当 NSNotificationCenter 注册一个通知后
- (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;
复制代码
进行注销。
不能用 - (void)removeObserver:(id)observer;
进行通知的注销。
注意: 若是不注销,将致使class不会被释放。
H5开发的弊端: 占用内容太多。在打开H5的时候,会占用大量的内存,在项目中看到,通常会达到一个页面50M的内存。
设置left bar button后,会致使右滑返回的效果失效,查看完美的设置方案。
同时为了获取到右滑返回的事件,能够执行
[self.navigationController.interactivePopGestureRecognizer addTarget:self action:@selector(back)];
复制代码
**在ViewController中viewDidAppare中添加,在viewWillDisappear中remove。 **
[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);
}];
复制代码
设置self.bottomOpenStoreBtn的顶部与promptImageView的顶部距离30pt make.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);
}
复制代码
即一个是layout属性,另外一个为frame的属性。