Tuesday 24 May 2011

Custom Tabbar for iOS

// mytabbar.h
#import

@class AppDelegate;

@interface mytabbar :UITabBarController
{
       AppDelegate *appDelegate;
       NSMutableArray *arrayofButton;
       BOOL isRotate;
}
@property (nonatomic,retain) NSMutableArray *arrayofButton;
-(void) tabBarAction:(UIButton*)sender;
-(void) setFrameForPortrait;
-(void) setFrameForLandScap;
@end

*******************
// mytabbar.m
*******************
#import mytabbar.h"
#import "myvc1.h"
#import "myvc2.h"


@implementation mytabbar
@synthesize arrayofButton;

-(void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:YES];
}

- (void)viewDidLoad {

 [super viewDidLoad];
   appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
   self.tabBarController.delegate=self;
   arrayofButton = [[NSMutableArray alloc] init];
   self.view.autoresizesSubviews=YES;
   self.view.autoresizingMask=UIViewAutoresizingFlexibleHeight||UIViewAutoresizingFlexibleWidth;
   NSMutableArray *controllerarray=[[NSMutableArray alloc] init];
   UIImageView *tabBarBG=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Toll_bar.png"]];
  tabBarBG.frame=CGRectMake(0, 0, 1024,50);
  [self.tabBar addSubview:tabBarBG];

   myvc1 *loc=[[myvc1 alloc] initWithNibName:@"myvc1" bundle:nil];
   UINavigationController *nav_loc=[[UINavigationController alloc] initWithRootViewController:loc];
   [nav_loc setNavigationBarHidden:FALSE];
   loc.tabBarItem.image=[UIImage imageNamed:@"tabTempImage1.png"];
   loc.tabBarItem.title=@"Locations";
   [controllerarray addObject:loc];
   [nav_loc release];
   [loc release];

   myvc2 *loc2=[[myvc1 alloc] initWithNibName:@"myvc1" bundle:nil];
   UINavigationController *nav_loc2=[[UINavigationController alloc] initWithRootViewController:loc2];
   [nav_loc2 setNavigationBarHidden:FALSE];
   loc2.tabBarItem.image=[UIImage imageNamed:@"tabTempImage1.png"];
   loc2.tabBarItem.title=@"Locations";
   [controllerarray addObject:loc2];
   [nav_loc2 release];
   [loc2 release];

   self.viewControllers=controllerarray;
   [controllerarray release];
   isRotate=NO;
}


-(void)viewDidAppear:(BOOL)animated
{
 [super viewDidAppear:YES];
 UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];

 [self.tabBar addSubview:btn1];

 [btn1 setImage:[UIImage imageNamed:@"Tab0.png"] forState:UIControlStateNormal];
 [btn1 setImage:[UIImage imageNamed:@"Tab0_s.png"] forState:UIControlStateSelected];
 btn1.tag = 0;
 btn1.frame = CGRectMake(0+109,0,110, 48);
 [btn1 addTarget:self action:@selector(tabBarAction:) forControlEvents:UIControlEventTouchUpInside];
 [arrayofButton addObject:btn1];

 UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
 [self.tabBar addSubview:btn2];
 [btn2 setImage:[UIImage imageNamed:@"Tab1.png"] forState:UIControlStateNormal];
 [btn2 setImage:[UIImage imageNamed:@"Tab1_s.png"] forState:UIControlStateSelected];

 //[btn2 setTitle:@"Sosh" forState:UIControlStateNormal];
 btn2.tag = 1;
 btn2.frame = CGRectMake(219,0,110, 48);
 [btn2 addTarget:self action:@selector(tabBarAction:) forControlEvents:UIControlEventTouchUpInside];
 [arrayofButton addObject:btn2];

 [btn2 setSelected:YES];

 self.selectedIndex=1;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
 [super didReceiveMemoryWarning];

// Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
 [super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
  [super dealloc];
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
 UIButton *b = [arrayofButton objectAtIndex:tabBarController.selectedIndex];
 [self tabBarAction:b];

}

-(void) tabBarAction:(UIButton*)sender{

  UIButton *btn = sender;

  self.selectedIndex = btn.tag;
  for (int i=0; i < [arrayofButton count]; i++) {
  UIButton *b = [arrayofButton objectAtIndex:i];
  [b setSelected:NO];

}
 self.selectedIndex =0;
 self.selectedIndex=btn.tag;
 [btn setSelected:YES];

}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{

if( interfaceOrientation == UIInterfaceOrientationPortrait||interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown )
{ [self setFrameForPortrait];



}
if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft||interfaceOrientation==UIInterfaceOrientationLandscapeRight)
{
   [self setFrameForLandScap];
}
return YES;


}
-(void) setFrameForPortrait{
 int temp=109;
 NSLog(@"setFrameForPortrait=%f",  self.tabBarController.tabBar.frame.origin.x);
 //[self.tabBarController.tabBar setFrame:CGRectMake(0, 0, 768, 50)];
 for (int i=0; i < [arrayofButton count] ; i++)
{
  UIButton *b = [arrayofButton objectAtIndex:i];
   if(i==0)
   {
     b.frame = CGRectMake(i+temp, 0, 110, 48);
   }else {
     b.frame = CGRectMake(i*110+temp, 0, 110, 48);
  }
 }
}

-(void) setFrameForLandScap{
  CGRect cg=self.tabBarController.selectedViewController.view.frame;
  NSLog(@"setFrameForLandScap=%@", NSStringFromCGRect(self.tabBarController.tabBar.frame));
  //[self.tabBarController.tabBar setFrame:CGRectMake(0, 0, 1024, 50)];
  NSLog(@"%@",NSStringFromCGRect(cg));
  int temp=237;
  for (int i=0; i < [arrayofButton count] ; i++) {
    UIButton *b = [arrayofButton objectAtIndex:i];
     if(i==0)
    {
     b.frame = CGRectMake(i+temp, 0, 110, 48);
    }else {
     b.frame = CGRectMake(i*110+temp,0, 110, 48);
   }
}
}

@end

Friday 20 May 2011

Twitter integration with OAuth for iOS

Step by step guide for connecting to twitter with OAuth.
You can download source code as well as SDK from below link:

More..

iOS interview questions

What is the difference between underscore and self (i.e self . xx and _xx)  ? What is property? What is the difference between weak and ...