UIPickerview in iPhone? - iphone

I m new to objective-C and I need to create a UIPickerView when a particular textfield is touched that would be populated with the items of NSMutableArray..I have implemented this code but couldnot see the pickerView addded up.I can still see the keyboard popping up when a textfield is touched...Where Am I gng wrong..???
- (BOOL)textFieldDidBeginEditing:(UITextField *)textField {
if( textField == txtstate)
{
[txtstate resignFirstResponder];
pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
[self.view addSubview:pickerView];
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
[pickerView selectRow:1 inComponent:0 animated:NO];
txtstate.text= [[arr objectAtIndex:row] valueForKey:#"Code"];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [arr count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [arr objectAtIndex:row];
}
My Array looks like this :
(
{
Code = Wisconsin;
Id = "6215b339-2959-4a3d-9430-f5bddb0a4eb9";
},
{
Code = Wyoming;
Id = "7972f963-9b3b-4ba6-a30c-d68bb9f59145";
}
)
Any help would be appreciated...

u can add button on UITextField and add your picker at that button action .
make sure your button should be custom type button and put that button over text field.

if you not give the delegate to UITextField name txtstate then give the delegate like bellow in viewDidLoad: method
txtstate.delegate = self;

Related

3 UIPicker view in single view controller

i have 3 picker view in my view controller
the picker1 contain contain 2 row Audio and Video.picker2 contain product type,if audio is selected in picker1 then picker2 should populate with (transistor,walkman,ipod) and if video is selected in picker1 then picker2 should populate with (tv,screen,monitor) and if walkman is selected in picker2 then picker3 should populate with (sony,philips,jbl).
i am confused with the if condition....
if ([[categoryArray objectAtIndex:row] isEqual:#"Audio"] && [[audioProduct objectAtIndex:row] isEqual:#"walkman"])
{
NSLog(#"Audio and walkman");
modelArray=[[NSMutableArray alloc]initWithObjects:#"walkman1",#"walkman2",#"walman3", nil];
[modelPickerView reloadAllComponents];
}
else if ([[categoryArray objectAtIndex:row] isEqual:#"Audio"] && [[audioProduct objectAtIndex:row] isEqual:#"mp3"]) {
modelArray=[[NSMutableArray alloc]initWithObjects:#"mp3",#"mp3", #"mp3",#"mp3",nil];
[modelPickerView reloadAllComponents];
}
you have to load picker2 and picker 3 conditionally, as follows:
picker1Array=[[NSMutableArray alloc] initWithObjects:#"Audio",#"Video", nil]
if ([[picker1array objectAtIndex:[picker1 selectedRowInComponent:0]] isEqual:#"Audio"])
{
picker2Array=[[NSMutableArray alloc] initWithObjects:#"transistor",#"walkman",#"ipod", nil];
}
else {
picker2Array=[[NSMutableArray alloc] initWithObjects:#"tv",#"screen",#"monitor", nil];
}
[picker2 reloadAllComponents];
if ([[picker2array objectAtIndex:[picker2 selectedRowInComponent:0]] isEqual:#"walkman"]){
picker3Array=[[NSMutableArray alloc] initWithObjects:#"sony",#"philips",#"jbl", nil];
}
[picker3 reloadAllComponents];
please try the following code.
in .h file please declare
NSMutableArray *randCatSel;
NSMutableArray *randSubCatSel;
NSMutableArray *randItemSel;
UIPickerView *packerView;
and in .m file write this code.
- (void)fetchedData{
randCatSel =[[NSMutableArray alloc] initWithObjects:#"Audio",#"Video", nil];
packerView=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 280, 320, 200)];
packerView.delegate=self;
packerView.dataSource=self;
packerView.tag=1;
[self.view addSubview:packerView];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
if(pickerView.tag==1)
{
return [randCatSel count];
}
else if(pickerView.tag==2)
{
return [randSubCatSel count];
}
else if(pickerView.tag==3)
{
return [randItemSel count];
}
else
{
return 0;
}
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(pickerView.tag==1)
{
return [randCatSel objectAtIndex:row];
}
else if(pickerView.tag==2)
{
return [randSubCatSel objectAtIndex:row];
}
else if(pickerView.tag==3)
{
return [randItemSel objectAtIndex:row];
}
else
{
return 0;
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(pickerView.tag==1)
{
if(randSubCatSel.count>0)
{
randSubCatSel=nil;
}
if([[randCatSel objectAtIndex:row] isEqualToString:#"Audio"])
{
randSubCatSel=[[NSMutableArray alloc] initWithObjects:#"Transistor",#"walkman",#"ipod", nil];
}
else
{
randSubCatSel=[[NSMutableArray alloc] initWithObjects:#"TV",#"Screen",#"Monitor", nil];
}
[packerView removeFromSuperview];
packerView=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 280, 320, 200)];
packerView.delegate=self;
packerView.dataSource=self;
packerView.tag=2;
[self.view addSubview:packerView];
}
else if(pickerView.tag==2)
{
//do coding according to randSubCatSel for randItemSel
}
}
please note that this is sample code for your requirement. you need to make changes accordingly.
i hope this code will help you.

How to change uipickerview in landscape?

Here i developing application. I could not change pickerview in landscape mode. Here is my code.
-(IBAction) showPicker:(UIControl *)sender
{
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#""
delegate:self
cancelButtonTitle:#"Done"
destructiveButtonTitle:nil
otherButtonTitles:nil];
menu.tag=1;
UIPickerView *pickerView;
if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(-80,70,220,0)];
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
[menu addSubview:pickerView];
[menu showInView:self.view.superview];
[menu setBounds:CGRectMake(0,0,320, 470)];
[pickerView release];
[menu release];
NSLog(#"Landscape");
}
else
{
NSLog(#"Portrait");
pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,70,220,0)];
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
[menu addSubview:pickerView];
[menu showInView:self.view.superview];
[menu setBounds:CGRectMake(0,0,320, 470)];
[pickerView release];
[menu release];
}
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [arrayNo count];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
[entered2 resignFirstResponder];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
entered2.text=[arrayNo objectAtIndex:row];
[entered2 resignFirstResponder];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [arrayNo objectAtIndex:row];
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
int sectionWidth = 300;
return sectionWidth;
}
In my application i created portrait mode ran fine. But when i try to write code in landscape mode it can positioned wrongly. How can i do this.
How can i change landscape mode. Anybody help me. Thanks in advance.
You just need to set size inspector attributes like this:
P.S. Edit 1:You have to return yes in
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
Edit 2:
If you are doing like this you don't need if condition for checking orientation and make frame for it.

uipicker view implementation on action sheet

I have implemented the UIPicker view concept on action sheet, it is working properly but one thing it is not displaying properly that is row index. There is no highlighted row in the picker view.
I have uploaded the image. Please give me the solution for it.
Following is the code which I am using:
-(IBAction)clickformedtype:(id)sender
{
UIActionSheet *asheet = [[UIActionSheet alloc] initWithTitle:#"Select medication type" delegate:self
cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Select", nil];
[asheet showInView:[self.view superview]]; //note: in most cases this would be just self.view, but because I was doing this in a tabBar Application, I use the superview.
[asheet setFrame:CGRectMake(0, 117, 320, 383)];
[asheet release];
}
-(void)willPresentActionSheet:(UIActionSheet *)actionSheet {
medicationtypepicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];
medicationtypepicker.delegate = self;
medicinetypearray = [[NSMutableArray alloc]initWithObjects:#"ml",#"capsules",#"eyedrops", nil];
//medicationtypepicker = [[NSMutableArray alloc]initWithObjects:#"ml",#"capsules",#"eyedrops", nil];
//medicationtypepicker.datePickerMode = ui;
//Configure picker...
// [medicationtypepicker setMinuteInterval:1];
[medicationtypepicker setTag: kDatePickerTag];
//Add picker to action sheet
[actionSheet addSubview:medicationtypepicker];
// [medicationtypepicker release];
//Gets an array af all of the subviews of our actionSheet
NSArray *subviews = [actionSheet subviews];
[[subviews objectAtIndex:SelectButtonIndex] setFrame:CGRectMake(20, 266, 280, 46)];
[[subviews objectAtIndex:CancelButtonIndex] setFrame:CGRectMake(20, 317, 280, 46)];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
UIPickerView
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [medicinetypearray count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [medicinetypearray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
medicinetype.text=[medicinetypearray objectAtIndex:row];
//medicationtypepicker.hidden = YES;
}
Thanks in advance.
UIPickerView has a property called "showsSelectionIndicator" - set this to YES also set autoresizingMask to UIViewAutoresizingFlexibleWidth as follows...
mTimePicker.showsSelectionIndicator = YES;
mTimePicker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
Try to set property showsSelectionIndicator to YES:
medicationtypepicker.showsSelectionIndicator = YES;

how to add uipickerview as as subview to mainview in iphone?

I have one application in which when user clicks a button i want to open uipickerview as as subview to main view and after user selects an item it should be removed from main view.(drop down kind of functionality).For that i have written code as below:
-(void)showPrefPicker:(id)sender
{
UIView *subView=[[UIView alloc] init];
subView.frame=CGRectMake(180, 120, 150, 150);
pickerView = [[UIPickerView alloc] init];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
pickerView.frame=CGRectMake(190, 130, 100, 100);
subView.backgroundColor=[UIColor blackColor];
[subView addSubview:pickerView];
[self.view addSubview:subView];
[pickerView release];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(#"selected object is %#",[arraypref objectAtIndex:row]);
//[pickerView ]
//mlabel.text= [arrayNo objectAtIndex:row];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [arraypref count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [arraypref objectAtIndex:row];
}
but is only showing subview in the main view not the pickerview.how can i do that any tutorial or source code for that?
Better you use
to hide
pickerview.hidden=YES;
to show
pickerview.hidden=NO;
Try changing the frame of the pickerView as
pickerView.frame = CGRectMake(10,10,100,100);
in your code subView.frame=CGRectMake(180, 120, 150, 150);
and pickerView.frame=CGRectMake(190, 130, 100, 100);
picker view is a subview of your 'subView'
here pickerview's frame starts out of bounds of subview ie origin x is 190. but the width of the subview is only 150.
so the correct code is
pickerView.frame=CGRectMake(0, 0, 100, 100);
since pickerview is a subview of a custom view. it should be in the frame of its parent view.

getting confusion with how to open Two array values for Two Button click events in UIPicker view

I need to display drop down box in my app.
most of them suggest me UIPickerView for the drop down box.
But my requirement is i need to place two drop down boxes in my app.
MY code for UIPicker view is
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [arrayColors count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [arrayColors objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(#"Selected Color: %#. Index of selected color: %i", [arrayColors objectAtIndex:row], row);
}
- (IBAction)dropdown_term_in_years: (id)sender
{
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Ratings"
delegate:self
cancelButtonTitle:#"OK"
destructiveButtonTitle:nil
otherButtonTitles:nil];
UIPickerView *pickerView = [[UIPickerView alloc] init];
//pickerView.datePickerMode = UIDatePickerModeDate;
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[menu addSubview:pickerView];
[menu showInView:self.view];
[menu sendSubviewToBack:pickerView];
[menu setBounds:CGRectMake(0,0,320, 300)];
CGRect pickerRect = pickerView.bounds;
//pickerRect.origin.y = -100;
pickerView.bounds = pickerRect;
[pickerView release];
[menu release];
}
this works for one button click, I need to open another array values for the another button click.
how can i done this.
PLs help me.
Thank U in advance.
I think you need tag assignment for all picker view.
Assign two different tag for both picker view.
Now simply get tag from thePickerView argument and take respective action inside delegate.
i.e
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
switch(thePickerView.tag){
case 101:
return [arrayImage count];
case 102:
return [arrayColors count];
}
   
}