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.
Related
i have a text field and picker view.when i select the text field picker view is appearing and if i select row in picker still my textfield is editable. i want to set textfield only in drop down list.and one more problem my key board is visible when i select the textfield
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[txtstate resignFirstResponder];//this is to hide key board
txtstate.inputView=pickerView;
ViewForValuePicker = [[UIView alloc]initWithFrame:CGRectMake(43,337, 212, 160)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(43, 0, 212, 30)];
toolBar.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneBtnPressToGetValue)];
[toolBar setItems:[NSArray arrayWithObject:btn]];
[ViewForValuePicker addSubview:toolBar];
pickerView = [[UIPickerView alloc]init] ;
pickerView.frame=CGRectMake(43, 30, 212, 140);
pickerView.delegate = self;
pickerView.dataSource= self;
pickerView.showsSelectionIndicator = YES;
[ViewForValuePicker addSubview:pickerView];
errstate.hidden=YES;
[testScroll addSubview:ViewForValuePicker];
[pickerView setHidden:NO];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *retval = (id)view;
if (!retval) {
retval= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f,
[pickerView rowSizeForComponent:component].width,
[pickerView rowSizeForComponent:component].height)] ;
}
retval.text = [[arr objectAtIndex:row]objectForKey:#"Code"];
retval.font = [UIFont fontWithName:#"Helvetica-Bold" size:12];
return retval;
}
- (void)doneBtnPressToGetValue{
[pickerView resignFirstResponder];
[pickerView removeFromSuperview];
txtcity.text=nil;
autocompleteTableView.hidden=YES;
[ ViewForValuePicker removeFromSuperview];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
txtLId.text=[[arr objectAtIndex:row] valueForKey:#"LId"];
txtstate.text= [[arr objectAtIndex:row] valueForKey:#"Code"];
//txtstate.userInteractionEnabled=NO;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [arr count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [[arr objectAtIndex:row] valueForKey:#"Code"];
}
Hello try and update following code with your code or check full code at http://pastie.org/6569798 :-
First define
UIActionSheet *actionSheet;
in header file(.h).
- (void)createActionSheet {
if (actionSheet == nil)
{
actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
[flexSpace release];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneBtnPressToGetValue)];
[barItems addObject:doneBtn];
[doneBtn release];
[pickerToolbar setItems:barItems animated:YES];
[barItems release];
[actionSheet addSubview:pickerToolbar];
[pickerToolbar release];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0,0,320, 464)];
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if(textField==txtstate)
{
pickerView = [[UIPickerView alloc]init] ;
pickerView.frame=CGRectMake(0, 44, 320, 420);
pickerView.delegate = self;
pickerView.dataSource= self;
pickerView.showsSelectionIndicator = YES;
[pickerView setHidden:NO];
[self createActionSheet];
[actionSheet addSubview:pickerView];
[textField resignFirstResponder];
}
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *retval = (id)view;
if (!retval) {
retval= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f,
[pickerView rowSizeForComponent:component].width,
[pickerView rowSizeForComponent:component].height)] ;
}
retval.text = [arr objectAtIndex:row];
retval.font = [UIFont fontWithName:#"Helvetica-Bold" size:12];
return retval;
}
- (void)doneBtnPressToGetValue
{
[txtstate resignFirstResponder];
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
[actionSheet release];
actionSheet = nil;
}
Assign your textfield.inputview in viewDidLoad itself with pickerView.
In textFieldDidBeginEditing - no more code required.
Also use your UIPickerViewDelegate methods as it is.
If you want to have just the pickerview show up when you select the text field, take all of the code you have right now in textFieldDidBeginEditing and move it into viewDidLoad. If you initialize txtstate.inputView=pickerView; before hand it will replace the keyboard as the main input.
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;
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;
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];
}
}
I have 2 instances of UITextField and 1 instance of UIPickerView. UIPickerView is able to update data into
UITextField in didSelectRow:(NSInteger)row inComponent:(NSInteger)component. But how do I update 2 UITextField from UIPickerView when the user touch the UITextView?
Thanks. :)
you will need to make the UIPickerView the inputView of the textfields, i have made an example.
.h file:
#interface aViewController : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource> {
UITextField * textField0;
UITextField * textField1;
UIPickerView * pickerView;
UIToolbar * toolBar;
NSArray * items0;
NSArray * items1;
}
-(void)cancel;
-(void)close;
#end
.m need something like the following:
- (void)viewDidLoad
{
[super viewDidLoad];
textField0 = [[UITextField alloc] initWithFrame:CGRectMake(10.f, 20.0f, 100.f, 30.f)];
textField1 = [[UITextField alloc] initWithFrame:CGRectMake(130.f, 20.0f, 100.f, 30.f)];
textField0.backgroundColor = [UIColor grayColor];
textField1.backgroundColor = [UIColor grayColor];
pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320.f, 200.f)];
// Do any additional setup after loading the view from its nib.
[self.view addSubview:textField0];
[self.view addSubview:textField1];
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320.f, 44.f)];
UIBarButtonItem * cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancel)];
UIBarButtonItem * doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(close)];
toolBar.items = [NSArray arrayWithObjects:doneButton,cancelButton, nil];
items0 = [[NSArray alloc] initWithObjects:#"dogs",#"cats",#"llamas",#"emus", nil];
items1 = [[NSArray alloc] initWithObjects:#"bone",#"catnip",#"hay", nil];
[doneButton release];
[cancelButton release];
pickerView.showsSelectionIndicator = YES;
pickerView.delegate = self;
pickerView.dataSource = self;
textField1.inputView = pickerView;
textField0.inputView = pickerView;
textField1.inputAccessoryView = toolBar;
textField0.inputAccessoryView = toolBar;
}
#pragma mark - actions
-(void)cancel
{
//reset the values to the original values on cancel...
//do that here.
[textField0 resignFirstResponder];
[textField1 resignFirstResponder];
}
-(void)close
{
textField0.text = [items0 objectAtIndex:[pickerView selectedRowInComponent:0]] ;
textField1.text = [items1 objectAtIndex:[pickerView selectedRowInComponent:1]] ;
[textField0 resignFirstResponder];
[textField1 resignFirstResponder];
}
#pragma mark - pickerview handling
-(float)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
return 130.f;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component==0) {
return [items0 objectAtIndex:row];
}
return [items1 objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component==0) {
textField0.text = [items0 objectAtIndex:row];
return;
}
textField1.text = [items1 objectAtIndex:row];
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component ==0) {
return [items0 count];
}
return [items1 count];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
I haven't included any clean-up code.