Search This Blog

Thursday 16 June 2011

Customized Drop Down Menu in iPhone

Hi Friends,


This way i have created Drop Down Menu in iPhone.
As there is no as such Drop Down Menu available in iPhone,I used UITableView to show list of items.
You will get this look when you run the code initially,






When you click on the Button,you will see next window.





It is very simple to implement.
Create New Project,Add New File with .xib file.I created with dropdown name.


Steps :-


1.Include following code in dropdown.h file as follows,


#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface SimpleTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{
 IBOutlet UITableView *tblSimpleTable;
 IBOutlet UIButton *btn;
 IBOutlet UIImageView *i;
 BOOL flag;
 NSArray *arryData;
}
@property(nonatomic,retain)IBOutlet UITableView *tblSimpleTable;
@property(nonatomic,retain)IBOutlet UIButton *btn;
@property(nonatomic,retain)IBOutlet UIImageView *i;

-(IBAction)btnClicked;
@end






2.Add following code to dropdown.m file as follows,

#import "SimpleTableViewController.h"

@implementation SimpleTableViewController
@synthesize btn;
@synthesize tblSimpleTable;
@synthesize i;
@synthesize arryData;


-(IBAction)btnClicked{
 if (flag==1) {
  flag=0;
  tblSimpleTable.hidden=NO;
  [i setImage:[UIImage imageNamed:@"UpArrow.png"]];
 }
 else{
  flag=1;
  tblSimpleTable.hidden=YES;
  [i setImage:[UIImage imageNamed:@"downArrow.png"]];
 }
}

// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization

    }
    return self;
}

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
 arryData = [[NSArray alloc] initWithObjects:@"",@"iPhone",@"iPod",@"MacBook",@"MacBook Pro",nil];
 flag=1;
 tblSimpleTable.hidden=YES;
 btn.layer.cornerRadius=8;
 tblSimpleTable.layer.cornerRadius=8;

    [super viewDidLoad];
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}



#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [arryData count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Set up the cell...
 cell.textLabel.font=[UIFont fontWithName:@"Arial" size:16];

 cell.textLabel.text = [arryData objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}

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

 self.btn=nil;
 self.tblSimpleTable=nil;
 self.i=nil;
 self.arryData=nil;

 [super viewDidUnload];

}


- (void)dealloc {
    [btn release];
 [tblSimpleTable release];
 [i release];
 [arryData release];

 [super dealloc];
}

@end




3.Go to dropdown.xib file and drag UIButton,UIImageView,UITableView on view.
4.Link all the controls on the form with the names of the controls written in .h file.
5.Write necessary code in appDelegate file.
6.Download any "downArrow.png" and "UpArrow.png" image from internet and Drag it in Resources folder.




Now You Can Run the Code......
Download the source code here.