iOS开发之最近小知识总结二

有好多都是基础知识,仅供参考,谢谢

1、autolayout 自动布局

autoLayout 需要在- (void)viewDidLoad 方法执行完后生效,所以需要在- (void)viewDidAppear:(BOOL)animated 方法中再进行frame的获取,此时才能取到正确的frame。

2、NSArray进行不定参数处理

+ (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;
}
3、no input file 错误
如果在编译的时候找不到文件,需要先在Build Phases中的Compile Sources 将不存在的文件删除,然后再将找不到的文件添加到project中。
4、cg,cf,ca,ui等开头类

你还可以看到其他名字打头的一些类,比如CF、CA、CG、UI等等,比如
CFStringTokenizer 这是个分词的东东
CALayer 这表示Core Animation的层
CGPoint 这表示一个点
UIImage 这表示iPhone里面的图片

CF说的是Core Foundation,CA说的是Core Animation,CG说的是Core Graphics,UI说的是iPhone的User Interface

5、file's owner 含义

file's owner 就是xib对应的类,如view对应的xib文件的file's owner对应的类就是viewcontroller的类。
file’s owner 是view和viewcontroller之间的对应关系的桥梁。(即,一个视图,如何知道自己的界面的操作应该由谁来响应)

6、NSString格式限定符


7、延长APP的启动时间
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [NSThread sleepForTimeInterval:3.0f]; return YES; }
8、UIAppearance 使用
使用UIAppearance进行外观的自定义。

[+ appearance]修改整个程序中某个class的外观

[[UINavigationBar appearance] setTintColor:myColor];

[+ appearanceWhenContainedIn:]
当某个class被包含在另外一个class内时,才修改外观。

 [[UILabel appearanceWhenContainedIn:[cusSearchBar class], nil] setTextColor:[UIColor redColor]];
9、将NSString转换成UTF8编码的NSString
在使用网络地址时,一般要先将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 stringByReplacingPercentEscapesU
10、NSDateFormatter设定日期格式AM /PM

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];
11、将图片中间部分放大

根据图片上下左右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设置的值不要上下或左右交叉,不然会出现中间为空白的情况。

12、UITextField 弹出UIDatePicker

设置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就行了~

13、UIScrollView中立即响应操作

delaysContentTouches 属性是UIScrollView中立即响应Touch事件。默认是YES,如果想点击后马上有反应,则将该值设置为NO。

14、NSNotificationCenter 注销

当 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不会被释放。










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hbblzjy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值