iOS常用方法

来源:互联网 发布:filter 不过滤css js 编辑:程序博客网 时间:2024/06/09 14:22
https://developer.apple.com/


alertView:
 UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"新建联系人" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UITextField * __block nameTextField;
    UITextField * __block numTextField;
    [alertC addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        nameTextField = textField;
    }];
    [alertC addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        numTextField = textField;
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
    }];
    UIAlertAction *addLinkman = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSDictionary *dic = @{@"name":nameTextField.text,@"num":numTextField.text};
        [_linkmanList addObject:dic];
        [self saveData];
        [_tabView reloadData];
        
    }];
    [alertC addAction:cancelAction];
    [alertC addAction:addLinkman];
    [self presentViewController:alertC animated:YES completion:^{
        
    }];


///不显示多余的cell线.
_tableView.tableFooterView = [[UIView alloc]init];

获取沙盒路径:
1: NSString *dbPath = [NSString stringWithFormat:@"%@/Documents/class.rdb",NSHomeDirectory()];

2: NSString *linkmanPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)objectAtIndex:0];//获取沙盒路径
//补全中间路径
NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:linkmanPath isDirectory:NULL]) {
        [fileManager createDirectoryAtPath:linkmanPath withIntermediateDirectories:YES attributes:nil error:NULL];
    }
linkmanPath = [NSString stringWithFormat:@"%@/linkman.plist",linkmanPath];


3:  NSString *path = [[NSBundle mainBundle] pathForResource:@"baidu" ofType:@"html"];
    NSData *data = [NSData dataWithContentsOfFile:path];


沙盒cache路径:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDir = [paths objectAtIndex:0];


AppDelegate 单例:
AppDelegate *myAppDelegate=[UIApplication sharedApplication].delegate;

手势:
 UIPanGestureRecognizer * panGR=[[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGR:)] autorelease];
    [_fImgV addGestureRecognizer:panGR];
     _fImgV.userInteractionEnabled = YES;//imageView必须加这句

定义Block:
typedef void(^Change_Color)(UIColor *);


URL转码:

[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]




定时器:


严格意义上讲 CADisplayLink 并不是计时器控件, 它是与显示器刷新频率一致的。比如显示的刷新频率是60赫兹, 那么 CADisplayLink 就会每秒钟执行60次。正是因为这个原因所以有时也把 CADisplayLink 当做计时器控件来使用。
if(theTimer == nil){
 theTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(MyTask)];
theTimer.frameInterval = 2;
[theTimer addToRunLoop: [NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
   }
 
//停用
[theTimer invalidate];
theTimer = nil;
}

1,创建一个定时器
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
        这种方式创建的NStimer需要和NSRunLoop配合使用,NSRunLoop是一种消息机制的处理模式。不推荐这种创建方法

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
1)暂停:
_timer.fireDate = [NSDate distantFuture];
2)继续:
_timer.fireDate = [NSDate distantPast];

动画:
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations NS_AVAILABLE_IOS(4_0);

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);
0 0
原创粉丝点击