Custom PickerView without XIB in Iphone


In this tutorial we will see the  how we can make and customize  the pickerview  without xib (programmatically) in Iphone . This is the Window based app.  Here are some steps for it…



1. Open the xcode & choose "File->New Project".

2. Select "Application" from left menu and then "Window-based Application".

3.Name your project as "coustomPicker" and save the project.

4.Now just select "coustemPickerAppDelegate.h" file from left menu.

 

 

#import <UIKit/UIKit.h>


@interface coustemPickerAppDelegate : NSObject <UIApplicationDelegate, UIPickerViewDelegate,UIPickerViewDataSource> {

    UIWindow *window;

UIPickerView *pickerView;

NSArray *items;

UILabel *label;

}


@property (nonatomic, retain) IBOutlet UIWindow *window;

@property(nonatomic,retain)UIPickerView *pickerView;

@property(nonatomic,retain)NSArray *items;

@property(nonatomic,retain)UILabel *label;

-(void)click;

@end

 

5. Now select "coustemPickerAppDelegate.m" file from menu for implementation.

 

#import "coustemPickerAppDelegate.h"


@implementation coustemPickerAppDelegate


@synthesize window,pickerView,items,label;



#pragma mark -

#pragma mark Application lifecycle


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    

items =[[NSArray alloc]initWithObjects:@"First",@"Second",@"Third",@"Four",@"Five",nil];

    // Override point for customization after application launch.

UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0,0,320,480)];

view.backgroundColor=[UIColor grayColor];

pickerView=[[UIPickerView alloc] initWithFrame:CGRectMake(10,100,150,150)];

pickerView.transform = CGAffineTransformMakeScale(0.75f, 0.75f);

pickerView.delegate = self;

pickerView.dataSource = self;

pickerView.showsSelectionIndicator = YES;

pickerView.backgroundColor = [UIColor clearColor];

[pickerView selectRow:10 inComponent:0 animated:YES];

UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];

button.frame=CGRectMake(120, 325, 100, 40);

[button setTitle:@"Click me!" forState:UIControlStateNormal];

[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];

[view addSubview:button];

label=[[UILabel alloc]initWithFrame:CGRectMake(100,60,150,40)];

label.text=@"selected items";

label.textAlignment=UITextAlignmentCenter;

label.backgroundColor=[UIColor grayColor];

[view addSubview:label];

[view addSubview:pickerView];

UINavigationBar *navigation=[[UINavigationBar alloc] initWithFrame:CGRectMake(0,10,320,50)];

navigation.barStyle=UIBarStyleBlackOpaque;

UILabel *navLabel = [[UILabel alloc] initWithFrame:

CGRectMake(90,20,150,30)];

navLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;

navLabel.text = @"Coustom Picker";

navLabel.backgroundColor = [UIColor blackColor];

navLabel.font = [UIFont systemFontOfSize:20];

navLabel.textColor=[UIColor whiteColor];

navLabel.textAlignment = UITextAlignmentCenter;

[navigation addSubview:navLabel];

[navLabel release];


[view addSubview:navigation];

[window addSubview:view];

    [window makeKeyAndVisible];

return YES;

}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;

{

return 1;

}





- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;

{

return [items count];

}


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

return[items objectAtIndex:row];

}


- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

UILabel *lbl = [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)] autorelease];

lbl.text = [items objectAtIndex:row];

lbl.adjustsFontSizeToFitWidth = YES;

lbl.textAlignment=UITextAlignmentCenter;

lbl.font=[UIFont systemFontOfSize:20];

return lbl;

}

-(void)click{


NSString *Value=[items objectAtIndex:[pickerView selectedRowInComponent:0]];

label.text=Value;


}

- (void)dealloc {

    [window release];

    [super dealloc];

}



@end

 

7. Then Quit and Save the Interface Builder.


8. Now you are ready for  click on "Build & Go" button. 


Now you can see the output in iphone simulator .


 



(1) Responses to this post

Melly May 05 2011 at 01:04:52

Cool! That's a cleevr way of looking at it!


Leave your comment here






User Login




New User Forgot password

Newsletter Subscribe

Advertisement

 
 
 
quick contact