Two pickers for 2 textfield in one view - iphone

I have 2 text fields. Each has own picker view.When a user clicks on each of them, UIPickerView appears. Everything works. However, when,first, I click on the second text field, picker view for the second text field appears, and first text field takes a result from this picker view, not the second one. Something is not right in my code. I cannot figure out what exactly. Please help...Thanks in advance)
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField == reminder){
choosen = reminderString;
mySheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[mySheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
myArray = [[NSMutableArray alloc]initWithObjects: #"15 minutes",#"20 minutes",#"30 minutes", #"45 minutes",nil];
CGRect pickerFrame = CGRectMake(0,44,0,0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.delegate =self;
pickerView.dataSource=self;
pickerView.showsSelectionIndicator = YES;
[mySheet addSubview:pickerView];
UIToolbar *controlToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, mySheet.bounds.size.width, 44)];
[controlToolBar setBarStyle:UIBarStyleBlack];
[controlToolBar sizeToFit];
UIBarButtonItem *spaser = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *setButton = [[UIBarButtonItem alloc] initWithTitle:#"set" style:UIBarButtonItemStyleDone target:self action:#selector(DatePickerDoneClick)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelRepetition)];
[controlToolBar setItems:[NSArray arrayWithObjects:spaser, setButton, cancelButton,nil] animated:NO];
[mySheet addSubview:controlToolBar];
[mySheet showInView:self.view];
[mySheet setBounds:CGRectMake(0, 0, 320, 485)];
return NO;
}
else if(textField == repetition){
choosen = repetitionString;
repetitionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[mySheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
myArray = [[NSMutableArray alloc] initWithObjects:#"One-time task",#"Daily",#"Weekly",#"Yearly",nil];
CGRect pickerFrame = CGRectMake(0,44,0,0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.delegate =self;
pickerView.dataSource=self;
pickerView.showsSelectionIndicator = YES;
[mySheet addSubview:pickerView];
UIToolbar *controlToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, mySheet.bounds.size.width, 44)];
[controlToolBar setBarStyle:UIBarStyleBlack];
[controlToolBar sizeToFit];
UIBarButtonItem *spaser = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *setButton = [[UIBarButtonItem alloc] initWithTitle:#"set" style:UIBarButtonItemStyleDone target:self action:#selector(DatePickerDoneClick)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelRepetition)];
[controlToolBar setItems:[NSArray arrayWithObjects:spaser, setButton, cancelButton,nil] animated:NO];
[mySheet addSubview:controlToolBar];
[mySheet showInView:self.view];
[mySheet setBounds:CGRectMake(0, 0, 320, 485)];
return NO;
}
else{
return 0;
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return myArray.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [myArray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (chosen == reminderString) {
reminderString =[myArray objectAtIndex:row];
[reminder setText:reminderString];
}
else if (chosen == repetitionString){
reminderString =[myArray objectAtIndex:row];
[repetition setText:reminderString];
}
}
- (void)DatePickerDoneClick{
[mySheet dismissWithClickedButtonIndex:0 animated:YES];
}
-(void)cancelRepetition{
[mySheet dismissWithClickedButtonIndex:0 animated:YES];
}

you can give the name to pickers and then distinguish between them for
ex Picker1 and picker2 and in didselectrow put the condition
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if(pickerview == picker1)
{
reminderString =[myArray objectAtIndex:row];
[reminder setText:reminderString];
}
elseif(pickerview == picker2)
{
reminderString =[myArray objectAtIndex:row];
[repetition setText:reminderString];
}

Related

action sheet hiding the button showing it in scroll view

I have a UIButton on a scroll view. On clicking that button action sheet appears. But the button gets hide behind the sheet. I want button to be scrolled up so that it remains visible above action sheet.
Please help
- (IBAction)foodInstructionOrPrescriptionInformationButtonClicked:(UIButton *)sender
{
tagForPicker = sender.tag;
[self presentPicker:sender];
}
- (void)presentPicker:(UIButton*)element
{
actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, 0, 0)];
picker.tag = tagForPicker;
picker.dataSource = self;
picker.delegate = self;
[actionSheet addSubview:picker];
UIToolbar *tools = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
tools.barStyle=UIBarStyleDefault;
[actionSheet addSubview:tools];
UIBarButtonItem *doneButton=[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(btnActionPickerDoneClicked)];
UIBarButtonItem *CancelButton=[[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(btnActionPickerCancelClicked)];
UIBarButtonItem *flexSpace= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *array = [[NSArray alloc]initWithObjects:CancelButton,flexSpace,flexSpace,doneButton,nil];
[tools setItems:array];
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0, 0, self.view.frame.size.width, 400)];
}
else
{
[actionSheet showFromRect:CGRectMake(0, 480, 320, 215) inView:self.view animated:YES];
[actionSheet setBounds:CGRectMake(0, 0, 320, 511)];
}
}
- (void)viewDidLayoutSubviews
{
[backgroundScrollView setContentSize:CGSizeMake(320,1340)];
}

display pickerview along with action sheet

I have a textfield. once I tap on that I want a UIPickerView to pop out and select an item from the UIPickerView and after once I am done with the selection I want to confirm the selection by tapping on an UIActionSheet which has confirm button on it.So my requirement is to have a UIActionSheet and beneath that a picker view. I am just a beginner in iOS development and I am still not familiar with the technical terminologies. So please bear with my informal way of asking questions.
Here is my block of code:
creating UIPickerView and UIActionSheet
UIActionSheet *confirmRoomSelectionAS =[[UIActionSheet alloc]initWithTitle:#"" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Done",nil];
UIPickerView *roomTypePicker=[[UIPickerView alloc]init];
[confirmRoomSelectionAS addSubview:roomTypePicker];
[confirmRoomSelectionAS showInView:self.view];
[confirmRoomSelectionAS setBounds:CGRectMake(0, 0, 320, 600)];
[roomTypePicker setFrame:CGRectMake(0, 170, 320, 216)];
[roomTypePicker setDataSource:self];
[roomTypePicker setDelegate: self];
roomTypePicker.showsSelectionIndicator = YES;
Try like this,
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[actionSheet showInView:[self.view window]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 390)];
[actionSheet addSubview:[self createToolbar:#"Test"]];
UIPickerView *picker = [[UIPickerView alloc] init];
picker.frame = CGRectMake(0, 44, 320, 162);
picker.showsSelectionIndicator = YES;
picker.dataSource = self;
picker.delegate = self;
[actionSheet addSubview:picker];
- (UIView *)createToolbar:(NSString *)titleString {
UIToolbar *inputAccessoryView = [[UIToolbar alloc] init];
inputAccessoryView.barStyle = UIBarStyleBlackOpaque;
inputAccessoryView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[inputAccessoryView sizeToFit];
CGRect frame = inputAccessoryView.frame;
frame.size.height = 44.0f;
inputAccessoryView.frame = frame;
UIBarButtonItem *doneBtn =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done:)];
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *cancelBtn =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancel:)];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0 , 11.0f, 100, 21.0f)];
[titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:14]];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setText:titleString];
[titleLabel setTextAlignment:UITextAlignmentCenter];
UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithCustomView:titleLabel];
NSMutableArray *array = [NSMutableArray arrayWithObjects:cancelBtn,flexibleSpaceLeft,title,flexibleSpaceLeft, doneBtn, nil];
[inputAccessoryView setItems:array];
[doneBtn release];
[title release];
[titleLabel release];
[flexibleSpaceLeft release];
[cancelBtn release];
return [inputAccessoryView autorelease];
}
- (void)done:(id)sender {
}
- (void)cancel:(id)sender {
}
It will be like,
3 things I thought you should modify:
1) add your cancel button manually, to later better check the cancel button pressing event
UIActionSheet * confirmRoomSelectionAS = [[UIActionSheet alloc]
initWithTitle:#""
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[confirmRoomSelectionAS addButtonWithTitle:#"Done"];
confirmRoomSelectionAS.cancelButtonIndex = [confirmRoomSelectionAS addButtonWithTitle: #"Cancel"];
[confirmRoomSelectionAS showInView:self.view];
2) implement your actionsheet delegate method
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == actionSheet.cancelButtonIndex) {
// cancel button pressed;
return;
}
}
3) You should declare *UIPickerView picker in your .h to later check of its selected value in the previous delegate method (also including the Done check) by adding sthing like this:
NSInteger row;
row = [picker selectedRowInComponent:0];

when picker view selected keyboard still visible

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.

UIPickerView with DONE button

I have a UIPickerView in my application, and I want to have a done button on it to hide the picker.
I don't want to use an action sheet because I want focus on the rest of the view.i mean all the view has to be active.
Here is how I create the picker:
UIPickerView *pickerViewCountry = [[UIPickerView alloc] init];
pickerViewCountry.showsSelectionIndicator = YES;
pickerViewCountry.dataSource = self;
pickerViewCountry.delegate = self;
pickerViewCountry.frame = CGRectMake(0,210 , 320, 15);
[self.view addSubview:pickerViewCountry];
[pickerViewCountry release];
try out the custom picker make with action sheet
UIActionSheet *actionSheet;
NSString *pickerType;
-(IBAction)BtnPressed:(id)sender
{
[self createActionSheet];
pickerType = #"picker";
select = NO;
UIPickerView *chPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
chPicker.dataSource = self;
chPicker.delegate = self;
chPicker.showsSelectionIndicator = YES;
[actionSheet addSubview:chPicker];
sessoTxt.text = [sessoArray objectAtIndex:0];
[chPicker release];
}
- (void)createActionSheet {
if (actionSheet == nil) {
// setup actionsheet to contain the UIPicker
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(pickerDone:)];
[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)];
}
}
#pragma mark UIPickerViewDelegate Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
int count;
if ([pickerType isEqualToString:#"picker"])
count = [array count];
return count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *string;
if ([pickerType isEqualToString:#"picker"])
string = [array objectAtIndex:row];
return string;
}
// Set the width of the component inside the picker
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return 300;
}
// Item picked
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
select = YES;
if ([pickerType isEqualToString:#"picker"])
{
Txt.text = [array objectAtIndex:row];
}
}
- (void)pickerDone:(id)sender
{
if(select == NO)
{
if ([pickerType isEqualToString:#"picker"])
{
Txt.text = [array objectAtIndex:0];
}
}
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
[actionSheet release];
actionSheet = nil;
}
}
You can add a toolbar to a UIPickerView like this:
// initiaize picker view
UIPickerView *pickerView=[[UIDatePicker alloc]init];//Date picker
pickerView.frame=CGRectMake(0,44,320, 216);
pickerView.datePickerMode = UIDatePickerModeDate;
[pickerView setMinuteInterval:5];
[self.view addsubview:pickerView]
toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered target:self action:#selector(changeDateFromLabel:)];
toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil];
barButtonDone.tintColor=[UIColor blackColor];
[pickerView addSubview:toolBar];
The picker view should be set as the inputView of the text field where you are entering the country. You should create a toolbar with a standard Done system item and set that as the inputAccessoryView of the same field.
You can do this by adding a UIToolBar with Done button with animation of UIPickerView.
I would even try using something like EAActionSheetPicker. It manages the display of a pickerView/datePicker inside of an actionSheet. It may just be what you're looking for.

UIPIckerView issue

I have the code below in my app. I show a UIPickerView with options, and I have a TextView placed on the same UIViewController .
when the picker is shown with options, the rest of the view seems not in focus and only when I resign the uipicker, the entire view gets focus.
what is the problem here?
-(void)viewWillAppear:(BOOL)animated {
myActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
UIPickerView *pickerViewCountry = [[UIPickerView alloc] init];
pickerViewCountry.showsSelectionIndicator = YES;
pickerViewCountry.dataSource = self;
pickerViewCountry.delegate = self;
pickerViewCountry.frame = CGRectMake(0,35 , 320, 15);
[myActionSheet addSubview:pickerViewCountry];
[pickerViewCountry release];
closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 27.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:#selector(hidePicker) forControlEvents:UIControlEventValueChanged];
[myActionSheet addSubview:closeButton];
[closeButton release];
[myActionSheet showInView:self.view];
[myActionSheet setBounds:CGRectMake(0, 0, 320, 250)];
UITextView *myTextView = [[UITextView alloc] init];
myTextView.frame = CGRectMake(100, 12, 210, 20);
myTextView.layer.borderWidth = 1.0f;
myTextView.layer.cornerRadius = 5;
myTextView.clipsToBounds = YES;
myTextView.layer.borderColor = [[UIColor grayColor] CGColor];
myTextView.text = #"some text";
[self.view addSubview:myTextView];
[myTextView sizeToFit];
}
You've placed your UIPickerView inside UIActionSheet. That's standart behavior for UIActionSheet. Try to use keyboardView instead like so:
UITextField* dummyTextField = [[UITextField alloc]initWithFrame:CGRectMake(0, -20, 10, 10)];//the point is to put it outside visible view
dummyTextField.inputView = pickerViewCountry;
[dummyTextField becomeFirstResponder];//call to appear
[dummyTextField resignFirstResponder];//call do disappear
and for the buttons you can use folloving:
dummyTextField.inputAccessoryView = <some toolbar with buttons or any other view>
Happy coding :)
I will suggest you one solution with custom View.
datePickerContainer is UIView:
//datepicker
datePickerContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 440, 310, 300)];
datePickerContainer.opaque = YES;
datePickerContainer.backgroundColor = [UIColor clearColor];
UIImageView *pickerNavBar = [[UIImageView alloc] init];
UIImage *barImage = [UIImage imageNamed:#"pickerbar.png"];
pickerNavBar.image = barImage;
pickerNavBar.frame = CGRectMake(0, 7, barImage.size.width, barImage.size.height);
[datePickerContainer addSubview:pickerNavBar];
[pickerNavBar release];
UIButton* blueButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[blueButton setTitle:#"Done" forState: UIControlStateNormal];
blueButton.frame = CGRectMake(255, 15, 55, 30);
[blueButton addTarget:self action:#selector(hidePickerViewContainer) forControlEvents:UIControlEventTouchUpInside];
[datePickerContainer addSubview:blueButton];
[blueButton release];
//UIPickerView Initialization code
UIPickerView *pickerViewCountry = [[UIPickerView alloc] init];
pickerViewCountry.showsSelectionIndicator = YES;
pickerViewCountry.dataSource = self;
pickerViewCountry.delegate = self;
pickerViewCountry.frame = CGRectMake(0,35 , 320, 15);
[datePickerContainer addSubview:pickerViewCountry];
[pickerViewCountry release];
//set container and release
[self.view addSubview: datePickerContainer];
[datePickerContainer release];
To show datePickerContainer UIVie use this code:
-(void)showPickerViewContainer{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
CGRect frameContainer = datePickerContainer.frame;
frameContainer.origin.y = 150.0f;
datePickerContainer.frame = frameContainer;
[UIView commitAnimations];
}
To handle done button and hide pickerView use this:
-(void)hidePickerViewContainer{
//here use you custom code to handle done action
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
CGRect frameContainer = datePickerContainer.frame;
frameContainer.origin.y = 440.0f;
datePickerContainer.frame = frameContainer;
[UIView commitAnimations];
}
This is just a custom solution. Hope this help.
declare variable in header file
UIActionSheet *actionSheet;
NSString *pickerType;
Write Below code into your implementation file:
- (void)createActionSheet {
if (actionSheet == nil) {
// setup actionsheet to contain the UIPicker
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(pickerDone:)];
[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)];
}
}
-(IBAction)BtnPressed:(id)sender
{
[self createActionSheet];
pickerType = #"picker";
select = NO;
UIPickerView *chPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
chPicker.dataSource = self;
chPicker.delegate = self;
chPicker.showsSelectionIndicator = YES;
[actionSheet addSubview:chPicker];
sessoTxt.text = [sessoArray objectAtIndex:0];
[chPicker release];
}
declare delegate methods
#pragma mark UIPickerViewDelegate Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
int count;
if ([pickerType isEqualToString:#"picker"])
count = [array count];
return count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *string;
if ([pickerType isEqualToString:#"picker"])
string = [array objectAtIndex:row];
return string;
}
// Set the width of the component inside the picker
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return 300;
}
// Item picked
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
select = YES;
if ([pickerType isEqualToString:#"picker"])
{
Txt.text = [array objectAtIndex:row];
}
}
- (void)pickerDone:(id)sender
{
if(select == NO)
{
if ([pickerType isEqualToString:#"picker"])
{
Txt.text = [array objectAtIndex:0];
}
}
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
[actionSheet release];
actionSheet = nil;
}
}