Wednesday, June 12, 2013

How to Add a Reload or Refresh to the top of your view in iOS 6 with ARC

This has been around for a while but apple standardized it finally.

in the .h file of your tableview or collectionview or pretty much any view;

@interface myViewController : UICollectionViewController {
    //all your other declarations
    UIRefreshControl *refreshControl;
}

In your .m file, wherever you initialize the view;
if (self) {
    //other special setup items once you know your view exists
    refreshControl = [[UIRefreshControl alloc] initWithFrame:CGRectMake(0, -44, 320, 44)];
    [refreshControl addTarget:self action:@selector(reload:) forControlEvents:UIControlEventValueChanged];
    [self.collectionView addSubview:refreshControl];
}

- (void) reload:(id) sender {
    //all your reload commands go here like [tableview reloaddata];
}


Different ways to differentiate between devices in iOS

There are many ways to figure out how to tell which device you are using, some are specific, some are general. They all serve their purpose. Here are some examples;

If you have a universal app and want to use a different nib for the ipad or iphone;
    NSString *nibName = [NSStringFromClass([self class]) stringByAppendingString:([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)? @"-iPhone" : @"-iPad"];
    self = [super initWithNibName:nibName bundle:nil];
* note be sure and name you nibs correctly.

If you have some specific code the you want to perform on either the iphone or ipad;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    //iphone code
}else{
    //ipad code
}

If you want a quick definition change depending on if it is an ipad or iphone;
    int total = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)? iPhone_Total :iPad_Total;

Thursday, June 6, 2013

Making something appear and disappear nicely in iOS6.0 with ARC

All UIViews have a hidden option that you can use to hide items when you don't want the user to see them but instead of just getting rid of them, you can make them appear and disappear nicely;

-(void)FlipView:(UIView*)flipView{

    if (flipView.hidden == FALSE){
        CGContextRef context = UIGraphicsGetCurrentContext();
        [UIView beginAnimations:nil context:context];
        [UIView setAnimationTransition: UIViewAnimationTransitionCurlUp forView:flipView cache:NO];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDuration:1.0];
        [UIView commitAnimations];
        
        flipView.hidden = TRUE;
    }else{
        CGContextRef context = UIGraphicsGetCurrentContext();
        [UIView beginAnimations:nil context:context];
        [UIView setAnimationTransition: UIViewAnimationTransitionCurlDown forView:flipView cache:NO];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDuration:1.0];
        [UIView commitAnimations];

        flipView.hidden = FALSE;
    }

}

The option UIViewAnimationTransitionCurlUp can be instead replaced with UIViewAnimationTransitionCurlDownUIViewAnimationTransitionFlipFromLeft, or UIViewAnimationTransitionFlipFromRight