ios代码片段收集 欢迎在评论里投稿

2013-01-31 01:05

ios代码片段收集 欢迎在评论里投稿

by admin

at 2013-01-30 17:05:44

original http://item.feedsky.com/~feedsky/helloJavaScript/~8514355/724967167/6618683/1/item.html

给button设置渐变背景

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = button.bounds;
gradient.colors = [NSArray arrayWithObjects:
(id)[UIColor colorWithRed:255/255.0f green:254/255.0f blue:249/255.0f alpha:1.0f].CGColor,
(id)[UIColor colorWithRed:250/255.0f green:240/255.0f blue:203/255.0f alpha:1.0f].CGColor,
nil];
[button.layer insertSublayer:gradient atIndex:0];

scrollview contentSize 自适应

{http://www.html-js.com/docs/ios-lib/AutoFitScrollView.zip}

AFJSONRequestOperation 请求网络json数据

    NSString *urlString = [NSString stringWithFormat:@"http://****?page=%d",page];
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        if([JSON objectForKey:@"success"]){
            // books = [NSMutableArray arrayWithCapacity:20];
            for (id key in [JSON objectForKey:@"data"]){
            }
            //状态栏的菊花
            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
            [self.tableView reloadData];
        }else{
            NSLog(@"load  error: %@", [JSON objectForKey:@"info"]);
        }
    } failure:^(NSURLRequest *request , NSURLResponse *response , NSError *error , id JSON){
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    }];
    [operation start];

storyboard用id寻找某个controller,然后push切换到它

UIStoryboard * storyboard = self.storyboard;
BookDetailViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:@"bookdetail"] ;    // ...
// Pass the selected object to the new view controller.
Book *book=[self.books objectAtIndex:indexPath.row];
detailViewController.book=book;
[self.navigationController pushViewController:detailViewController animated:YES];
detailViewController=nil;

动画隐藏tabbar

 
- (void) hideTabBar:(BOOL) hidden{
 
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
 
    for(UIView *view in self.tabBarController.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]])
        {
            if (hidden) {
                [view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
            } else {
                [view setFrame:CGRectMake(view.frame.origin.x, 480-49, view.frame.size.width, view.frame.size.height)];
            }
        }
        else
        {
            if (hidden) {
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
            } else {
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width,  480-49)];
            }
        }
    }
 
    [UIView commitAnimations];
}

在页面push切换的同时动画隐藏tabbar,二者不能同时进行,需要一个延时执行

-(void)hideTabbarTimeSelector
{
    [self hideTabBar:YES];
}
[self.navigationController pushViewController:detailViewController animated:YES];
[self performSelector:@selector(hideTabbarTimeSelector) withObject:nil afterDelay:0.3];

向NSUserDefaults写入信息

    NSUserDefaults  * userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:username forKey:@"username"];
    [userDefault setObject:password forKey:@"password"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    NSLog(@"写入用户信息:%@", userDefault);

关于block

http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/bxGettingStarted.html#//apple_ref/doc/uid/TP40007502-CH7-SW1

//不带参数的回调
+(void) loginWithSuccess:(void (^)())success failure:(void (^)())failure
{success();}
//带参数的回调
+ (instancetype)JSONRequestOperationWithRequest:(NSURLRequest *)urlRequest
        success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON))success
        failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON))failure
{
  success(**.request, **.response, responseObject);
}

iphone使用keychain来存取用户名和密码

http://blog.csdn.net/bl1988530/article/details/6887946